String manipulation in Python.

Strings are one of the most fundamental data types. Strings in Python can be expressed in a number of ways. The most common way to create a new string is to enclose text in single or double quotes. Strings in Python are immutable, which means that they cannot be modified once they are created.

>>> my_str = "Hello world. I am a Python string."

That is equivalent to writing:

>>> my_str = 'Hello world. I am a Python string.'

The backslash \ is used to escape quotes. For instance:

>>> my_str = 'Hello world. I\'m a Python string.'
"Hello world. I'm a Python string."

There may be times when you must include backslashes in your strings. The best way to do this is to turn the string into a raw string by prefacing it with anr:

>>> my_str = r'C:\home\user\admin'
>>> print(my_str)
C:\home\user\admin

Joining strings

Strings are joined together using + and can be repeated using *

>>> 'ba' + 'na' * 2
'banana'

Manipulating the strings

As a developer, handling strings of text is something you will do a lot and manipulating strings is something every developer must be familiar with. In Python, strings can be manipulated using built in string methods. It is important to note that the string methods I will talk about in this article do not modify strings in place but return modified copies of the strings.

Retrieve length of string

To retrieve the length of a given string, use the len() function:

>>> my_str
'Hello world. I am a Python string.'
>>> len(my_str)
34

The len function counts the number of characters in the string, including white space and periods.

Changing case

Python includes methods for changing the case of strings.
Let’s make our string all caps:

>>> my_str
'Hello world. I am a Python string.'
>>> my_str.upper()
'HELLO WORLD. I AM A PYTHON STRING.'

Next, let’s make it all lower case:

>>> my_str
'Hello world. I am a Python string.'
>>> my_str.lower()
'hello world. i am a python string.'

To check if a string is uppercase or lowercase use the isupper and islower methods.

>>> my_str
'Hello world. I am a Python string.'
lower_str = my_str.lower()
>>> lower_str
'hello world. i am a python string.'
>>> lower_str.islower()
True

>>> upper_str = my_str.upper()
>>> upper_str
'HELLO WORLD. I AM A PYTHON STRING.'
>>> upper_str.isupper()
True

To capitalise the first letter of the first word in a string

>>> lower_str
'hello world. i am a python string.'
>>> lower_str.capitalize()
'Hello world. i am a python string.'

To capitalise the first letter in each word

>>> lower_str
'hello world. i am a python string.'
>>> lower_str.title()
'Hello World. I Am A Python String.'

To reverse the case of all the letters

>>> my_str
'Hello world. I am a Python string.'
>>> my_str.swapcase()
'hELLO WORLD. i AM A pYTHON STRING.'

Manipulating string data

Sometimes, strings include characters you do not need. To remove these, use the strip() method. strip() removes unwanted leading and trailing characters. By default, strip removes whitespace, but it can be used to remove any character you tell it to.

>>> '  www.example.com   '.strip()
'www.example.com'


>>> 'www.example.com'.strip('wcom.')
'example'


It is also possible to add leading or trailing characters

>>> 'spaces'.ljust(10)
'spaces    '

>>> 'spaces'.rjust(10)
'    spaces'

These methods return a copy of the string justified to the left or to the right to the specified width. 'spaces'.rjust(10) tells Python to create a new string that is 10 characters long, push the word “spaces” to the right and replace the empty space with whitespace or any character the user specifies. This makes it possible to do fun things like

>>> ' this is a python comment'.rjust(26, '#')
'# this is a python comment'

Replacing whole words in strings is easy

>>> 'hello world!'.replace('world', 'universe')
'hello universe!'

Something you will do often is splitting strings. Splitting is useful for breaking a string down into parts. Use the split() method for this:

>>> 'this is a short string'.split()
['this', 'is', 'a', 'short', 'string']

Split returns a nice list you can manipulate even further. You can also tell split() where you want to split the string:


>>> 'this is a short string'.split('a')
['this is ', ' short string']

Lastly, we have join(). This is a personal favourite of mine. join is used to join strings in an iterable. I use the join() method mainly for making strings out of data contained in lists:

>>> my_list
['Hello', 'world.', 'I', 'am', 'a', 'Python', 'list.']
>>> ' '.join(my_list)
'Hello world. I am a Python list.'

Conclusion

Python supports many more string methods that I didn’t cover here. The one’s I discussed are the string methods I think are the most common. Feel free to dig into the Python Docs to find out more string methods. In a future article, I will cover how to manipulate strings using regular expressions.