Important and Useful Python String Methods

Sara Cemal
4 min readAug 12, 2021

--

Hi everyone! If you’ve been following me for a while, you know that one of my first few blogs consisted of me discussing differences between Python 2 and 3. Well, the Python learning hasn’t stopped there! Here and there, I’ve been taking a Codecademy course for Python, and I’ve been able to learn a lot along the way about the very popular programming language.

Like most object oriented programming languages, there are usually built in methods that you can use to help you with your coding. With Python, there are specific built in methods that you can use that are created just for strings.

Something to note about Python strings is that they are immutable — meaning that they can’t be changed. However, if you use one of these string methods, you can return the new and updated string while still keeping the integrity of the initial string! Not to mention that manipulating strings is pretty important in Python and we do it all the time. There are over 50 built in Python string methods, but we will go over a few that I think can be super beneficial for Python beginners (such as myself). :)

join()

The join() method is super helpful if you are looking to join different strings, in turn creating a new string of the combined strings. This can work on a single string, a list of strings, or a tuple of strings. What’s a tuple, you ask? A tuple is one of four data types in in python that stores multiple strings in one variable, functioning similarly to an array in Javascript.

The syntax is as follows:

separator.join(iterable) // the separator must be a string 

With the join keyword, you can join the strings with either a space, a comma, or colon. Let’s see an example:

// joining on single string
print('joining with commas:', ','.join('sara'))
// joining on list of strings
print('joining list of strings:', ' '.join(['I','am', 'Sara']))
// join on tuple of strings
print('joining tuple of strings with colon:', ':'.join(('Sara','hello', '100000')))

And here is the output:

joining with commas: s,a,r,a
joining list of strings: I am Sara
joining tuple of strings with colon:
Sara:hello:100000

find()

This method will search for a specific occurrence within a string, and it will return the first occurrence specified. There are optional parameters you may use to help narrow down even further the character that you are looking for.

Syntax:

string.find(value, start, end) ** start and end are optional parameters
  • value is going to be the value you are searching for.
  • start is where you want the starting point to be for the search.
  • end is where you want the ending point to be for your search.

If the value you are searching for is not available within the string you are searching, the output will return -1.

Let’s say that the character is found, the output will be the index of where the output is found. For example, here is a successful return method:

capitalize()

This one is pretty self-explanatory, but still helpful if you made an error and don’t want to have to go back to everryyyy place you called this, and that is capitalize! It takes no parameters, and will capitalize the first character in the string.

String = “my name is Sara!”
Cap = string.capitalize()
Print (x)
//output is “My name is Sara!”

Another good one… Let’s say you created a string but now you’re angry.

upper()

This will capitalize every character within the string.

string = “my name is Sara!”
yell = string.upper()
print (yell)
//output is “MY NAME IS SARA!”

replace()

Replace can be used to create a new string by replacing certain parts of a string. It takes three parameters, with one being optional.

The syntax looks like:

string.replace(search_string, replace_string, [counter])
  • search_string is the string we are searching for.
  • replace_string is what we are replacing the string with.
  • [counter] is optional, but it is there for you to identify how many occurrences of the string you want to replace. If you don’t specify, Python is going to assume that you want to replace them all.
string = “My name is Sara!”
change = string.replace(‘Sara’,’Tara’)
print (change)
//output is “My name is Tara!”

len()

Len() will take an input of a string and count all of the characters that are included within that string (punctuation, space, letters).

len(string)

Here is an example:


string = "My name is Sara!"
print(len(string))
//16

And of course there are many more. Next week maybe I’ll do a part two? Hope you enjoyed!

--

--

Sara Cemal

Flatiron School alumni with a sociology and neuroscience background.