Python String Manipulation Techniques
Python String Manipulation Techniques
com
2
String Manipulation in Python - Renan Moura - [Link]
Table of Contents
1. Preface
2. Basics
3. How to Split a String
4. How to remove all white spaces in a string
5. Multiline Strings
6. lstrip(): removing spaces and chars from the beginning of a string
7. rstrip(): removing spaces and chars from the end of a string
8. strip(): removing spaces and chars from the beginning and end of
a string
9. String Lowercase
10. String Uppercase
11. Title Case
12. Swap Case
13. Checking if a string is empty
14. rjust(): right-justified string
15. ljust(): left-justified string
16. isalnum(): checking alphanumeric only in a string
17. isprintable(): checking printable characters in a string
18. isspace(): checking white space only in a string
19. startswith(): checking if a string begins with a certain value
20. capitalize(): first character only to upper case in a string
21. isupper(): checking upper case only in a string
22. endswith(): check if a string ends with a certain value
23. join(): join items of an iterable into one string
24. splitlines(): splitting a string at line breaks
25. islower(): checking lower case only in a string
3
String Manipulation in Python - Renan Moura - [Link]
4
String Manipulation in Python - Renan Moura - [Link]
Preface
String manipulation is one of those activities in programming that we,
as programmers, do all the time.
In Python, on the other hand, you have several built-in functions in the
standard library to help you manipulate strings in the most different
ways you can think of.
If you come from another programming language, you will notice many
things that you can do with the standard library that are only possible
to do with the use of Regular Expressions in other languages.
Regulars Expressions are super powerful and useful, but can get really
hard to read, so having other alternatives is handy and helps with
keeping a more maintainable codebase.
Twitter: [Link]
LinkedIn: [Link]
Instagram: [Link]
5
String Manipulation in Python - Renan Moura - [Link]
Basics
The text type is one of the most common types out there and is often
called string or, in Python, just str.
<class 'str'>
<class 'str'>
<class 'str'>
Concatenate
You can use the + operator to concatenate strings.
Concatenation is when you have two or more strings and you want to
join them into one.
print(word1 + word2)
New York
Selecting a char
To select a char, use [] and specify the position of the char.
6
String Manipulation in Python - Renan Moura - [Link]
Size of a String
The len() function returns the length of a string.
>>> len('Rio')
3
>>> len('Rio de Janeiro')
14
Replacing
The replace() method replaces a part of the string with another. As an
example, let’s replace ‘Rio’ for ‘Mar’.
Rio means River in Portuguese and Mar means Sea, just so you know
I didn’t choose this replacement so randomly.
Count
Specify what to count as an argument.
7
String Manipulation in Python - Renan Moura - [Link]
Repeating a String
You can use the * symbol to repeat a string.
8
String Manipulation in Python - Renan Moura - [Link]
print(my_words)
#output:
#["let's", 'go', 'to', 'the', 'beach']
#output:
#let's
#go
9
String Manipulation in Python - Renan Moura - [Link]
#to
#the
#beach
As such, you might need to store some specific data from a certain
column.
my_csv = "mary;32;australia;mary@[Link]"
my_data = my_csv.split(";")
#output:
#mary
#32
#australia
#mary@[Link]
print(my_data[3])
#output:
# mary@[Link]
10
String Manipulation in Python - Renan Moura - [Link]
Notice the \s represents not only space ' ', but also form feed \f, line
feed \n, carriage return \r, tab \t, and vertical tab \v.
In summary, \s = [ \f\n\r\t\v].
import re
print(phrase)
# Do or do not there is no try
print(phrase_no_space)
#Doordonotthereisnotry
The original variable phrase remains the same, you have to assign the
new cleaned string to a new variable, phrase_no_space in this case.
11
String Manipulation in Python - Renan Moura - [Link]
Multiline Strings
Triple Quotes
To handle multiline strings in Python you use triple quotes, either
single or double.
print(long_text)
#output:
#This is a multiline,
#
#a long string with lots of text,
#
#I'm wrapping it in triple quotes to make it work.
print(long_text)
#output:
#This is a multiline,
#
#a long string with lots of text,
#
#I'm wrapping it in triple quotes to make it work.
Parentheses
12
String Manipulation in Python - Renan Moura - [Link]
As you can see, the result is not the same, to achieve new lines I have
to add \n, like this:
Backlashes
13
String Manipulation in Python - Renan Moura - [Link]
no_space_begin_text = regular_text.lstrip()
print(regular_text)
#' This is a regular text.'
print(no_space_begin_text)
#'This is a regular text.'
Removing Chars
The lstrip() method also accepts specific chars for removal as
parameters.
clean_begin_text = regular_text.lstrip("#$@G")
print(regular_text)
#$@G#This is a regular text.
print(clean_begin_text)
#This is a regular text.
14
String Manipulation in Python - Renan Moura - [Link]
no_space_end_text = regular_text.rstrip()
print(regular_text)
#'This is a regular text. '
print(no_space_end_text)
#'This is a regular text.'
clean_end_text = regular_text.rstrip("#$@G")
print(regular_text)
#This is a regular text.$@G#
print(clean_end_text)
#This is a regular text.
15
String Manipulation in Python - Renan Moura - [Link]
no_space_text = regular_text.strip()
print(regular_text)
#' This is a regular text. '
print(no_space_text)
#'This is a regular text.'
clean_text = regular_text.strip("AbC#$@G")
print(regular_text)
#AbC#This is a regular text.$@G#
print(clean_text)
#This is a regular text.
16
String Manipulation in Python - Renan Moura - [Link]
String Lowercase
Use the lower() method to transform a whole string into lowercase.
lower_case_text = regular_text.lower()
print(regular_text)
#This is a Regular TEXT.
print(lower_case_text)
#this is a regular text.
17
String Manipulation in Python - Renan Moura - [Link]
String Uppercase
Use the upper() method to transform a whole string into uppercase.
upper_case_text = regular_text.upper()
print(regular_text)
#This is a regular text.
print(upper_case_text)
#THIS IS A REGULAR TEXT.
18
String Manipulation in Python - Renan Moura - [Link]
Title Case
Use the title() method to transform the first letter in each word into
upper case and the rest of characters into lower case.
title_case_text = regular_text.title()
print(regular_text)
#This is a regular text.
print(title_case_text)
#This Is A Regular Text.
19
String Manipulation in Python - Renan Moura - [Link]
Swap Case
Use the swapcase() method to transform the upper case characters
into a lower case and vice versa.
swapped_case_text = regular_text.swapcase()
print(regular_text)
#This IS a reguLar text.
print(swapped_case_text)
#tHIS is A REGUlAR TEXT.
20
String Manipulation in Python - Renan Moura - [Link]
my_string = ''
if not my_string:
print("My string is empty!!!")
21
String Manipulation in Python - Renan Moura - [Link]
word = 'beach'
number_spaces = 32
word_justified = [Link](number_spaces)
print(word)
#'beach'
print(word_justified)
#' beach'
Notice the spaces in the second string. The word ‘beach’ has 5
characters, which gives us 27 spaces to fill with empty space.
word = 'beach'
number_chars = 32
char = '$'
print(word)
#beach
print(word_justified)
#$$$$$$$$$$$$$$$$$$$$$$$$$$$beach
22
String Manipulation in Python - Renan Moura - [Link]
word = 'beach'
number_spaces = 32
word_justified = [Link](number_spaces)
print(word)
#'beach'
print(word_justified)
#'beach '
Notice the spaces in the second string. The word ‘beach’ has 5
characters, which gives us 27 spaces to fill with empty space.
word = 'beach'
number_chars = 32
char = '$'
print(word)
#beach
print(word_justified)
#beach$$$$$$$$$$$$$$$$$$$$$$$$$$$
23
String Manipulation in Python - Renan Moura - [Link]
word = 'beach'
print([Link]())
#output: True
word = '32'
print([Link]())
#output: True
24
String Manipulation in Python - Renan Moura - [Link]
text = '' # notice this is an empty string, there is no white space here
print([Link]())
#output: True
text = '\f\n\r\t\v'
print([Link]())
#output: False
Notice that in the first 4 examples, all the character take some space,
even if it is an empty space as you could see in the first example.
The last example returns False, showing 5 kind of characters that are
non-printable: form feed \f, line feed \n, carriage return \r, tab \t, and
vertical tab \v.
25
String Manipulation in Python - Renan Moura - [Link]
text = '' # notice this is an empty string, there is no white space here
print([Link]())
#output: False
Notice in the second example that white space is not only ' ', but also
form feed \f, line feed \n, carriage return \r, tab \t, and vertical tab \v.
26
String Manipulation in Python - Renan Moura - [Link]
print([Link]('This is'))
#output: True
print([Link]('text'))
#output: False
You can also set if you want to begin the match in a specific position
and end it in another specific position of the string.
Finally, you might want to check for multiple strings at once, instead of
using some kind of loop, you can use a tuple as an argument with all
the strings you want to match against.
print([Link](('regular', 'This')))
#output: True
print([Link](('regular', 'text')))
#output: False
27
String Manipulation in Python - Renan Moura - [Link]
28
String Manipulation in Python - Renan Moura - [Link]
29
String Manipulation in Python - Renan Moura - [Link]
If you notice the last example, the numbers and special characters like
@ and $ in the string make no difference and isupper() still returns True
because the method only verifies the alphabetical characters.
30
String Manipulation in Python - Renan Moura - [Link]
print([Link]('regular text'))
#output: True
print([Link]('This'))
#output: False
You can also set if you want to begin the match in a specific position
and end it in another specific position of the string.
#look for in 'This is', the rest of the phrase is not included
print([Link]('This is', 0, 7))
#output: True
Finally, you might want to check for multiple strings at once, instead of
using some kind of loop, you can use a tuple as an argument with all
the strings you want to match against.
print([Link](('regular', 'is')))
#output: False
31
String Manipulation in Python - Renan Moura - [Link]
32
String Manipulation in Python - Renan Moura - [Link]
The method returns a new string, which means that the original iterator
remains unchanged.
Since the join() method only accepts strings, if any element in the
iterable is of a different type, an error will be thrown.
Let’s see some examples with: string, list, tuple, set, and dictionary
join(): Strings
The join() method puts the $ sign as a separator for every character
in the string.
my_string = 'beach'
print('$'.join(my_string))
#output: b$e$a$c$h
join(): Lists
I have a simple list of three items representing car brands.
33
String Manipulation in Python - Renan Moura - [Link]
It concatenates all the items on the list and puts the $ sign between
them.
print('$'.join(my_list))
#output: bmw$ferrari$mclaren
This another example remembers you that join() does not work with
non-string items.
my_list = [1, 2, 3]
print('$'.join(my_list))
#output:
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#TypeError: sequence item 0: expected str instance, int found
join(): Tuples
The tuple follows the same rationale as the list example explained
before.
print('$'.join(my_tuple))
#output: bmw$ferrari$mclaren
join(): Sets
Since the set is also the same as the tuple and the list, I’ve used a
different separator in this example.
34
String Manipulation in Python - Renan Moura - [Link]
join(): dictionaries
The dictionary has a catch when you use the join() method: it joins
the keys, not the values.
my_dict = {'bmw': 'BMW I8', 'ferrari': 'Ferrari F8', 'mclaren': 'McLaren 720S'}
print(','.join(my_dict))
#output: bmw,ferrari,mclaren
35
String Manipulation in Python - Renan Moura - [Link]
print(my_string.splitlines())
#output: ['world ', ' cup']
print(my_string.splitlines(True))
#output: ['world \n', ' cup']
36
String Manipulation in Python - Renan Moura - [Link]
If you notice the last example, the numbers and special characters like
@ and $ in the string make no difference and islower() still returns True
because the method only verifies the alphabetical characters.
37
String Manipulation in Python - Renan Moura - [Link]
word = '32'
print([Link]())
#output: True
word = 'beach'
print([Link]())
#output: False
word = 'number32'
print([Link]())
#output: False
38
String Manipulation in Python - Renan Moura - [Link]
word = '32'
print([Link]())
#output: True
word = 'beach'
print([Link]())
#output: False
word = 'number32'
print([Link]())
#output: False
39
String Manipulation in Python - Renan Moura - [Link]
word = '32'
print([Link]())
#output: True
word = '954'
print([Link]())
#output: True
word = 'beach'
print([Link]())
#output: False
word = 'number32'
print([Link]())
#output: False
40
String Manipulation in Python - Renan Moura - [Link]
word = 'beach'
print([Link]())
#output: True
word = '32'
print([Link]())
#output: False
word = 'number32'
print([Link]())
#output: False
41
String Manipulation in Python - Renan Moura - [Link]
If you notice the last example, the numbers and special characters like
@ and $ in the string make no difference and istitle() still returns True
because the method only verifies the alphabetical characters.
42
String Manipulation in Python - Renan Moura - [Link]
You can set any number of spaces, but when no argument is given,
the default is 8.
Basic Usage
my_string = 'B\tR'
print(my_string.expandtabs())
#output: B R
my_string = 'WORL\tD'
print(my_string.expandtabs())
#output: WORL D
The code below gives us 4 spaces for the first tab after four characters
‘WORL’ and 7 spaces for the second tab after one character ‘D’.
my_string = 'WORL\tD\tCUP'
43
String Manipulation in Python - Renan Moura - [Link]
print(my_string.expandtabs())
#output: WORL D CUP
Custom Tabsize
It is possible to set the tabsize as needed.
In this example the tabsize is 4, which gives us 3 spaces after the char
‘B’.
my_string = 'B\tR'
print(my_string.expandtabs(4))
#output: B R
This code has tabsize set to 6, which gives us 5 spaces after the char
‘B’.
my_string = 'B\tR'
print(my_string.expandtabs(6))
#output: B R
44
String Manipulation in Python - Renan Moura - [Link]
word = 'beach'
number_spaces = 32
word_centered = [Link](number_spaces)
print(word)
#'beach'
print(word_centered)
##output: ' beach '
Notice the spaces in the second string. The word ‘beach’ has 5
characters, which gives us 28 spaces to fill with empty space, 14
spaces before and 14 after to center the word.
word = 'beach'
number_chars = 33
char = '$'
print(word)
#beach
print(word_centered)
#output: $$$$$$$$$$$$$$beach$$$$$$$$$$$$$$
45
String Manipulation in Python - Renan Moura - [Link]
word = 'beach'
size_string = 32
word_zeros = [Link](size_string)
print(word)
#beach
print(word_zeros)
#000000000000000000000000000beach
Also notice that if the argument is less than the number of chars in the
string, nothing changes.
In the example below, ‘beach’ has 5 chars and we want to add zeros
until it reaches the size_string of 4, which means there is nothing to
be done.
word = 'beach'
size_string = 4
word_zeros = [Link](size_string)
print(word)
#beach
46
String Manipulation in Python - Renan Moura - [Link]
print(word_zeros)
#'beach'
47
String Manipulation in Python - Renan Moura - [Link]
The method returns the index of the first occurrence of the given value.
print([Link]('This'))
print([Link]('regular'))
print([Link]('text'))
0
10
18
print([Link]('train'))
-1
You can also choose to begin the search in a specific position and end
it in another specific position of the string.
#look for in 'This is', the rest of the phrase is not included
print([Link]('This', 0, 7))
48
String Manipulation in Python - Renan Moura - [Link]
print([Link]('regular', 0, 17))
0
10
8
49
String Manipulation in Python - Renan Moura - [Link]
This one is a pretty simple change and very friendly for beginners to
get used to reading the official documentation.
50
String Manipulation in Python - Renan Moura - [Link]
You can use the same rationale to distinguish between rstrip() and
removesuffix().
And as a bonus, just in case you have never worked with regular
expressions before, be grateful that you have strip() to trim character
51
String Manipulation in Python - Renan Moura - [Link]
>>> import re
>>> word = 'amazonia'
>>> [Link]('ami')
'zon'
>>> [Link]('^[ami]*(.*?)[ami]*$', word).group(1)
'zon'
52
String Manipulation in Python - Renan Moura - [Link]
Slicing
Slicing is one of the most useful tools in the Python language.
Basic Notation
Let’s say we have an array called ‘list’.
list[start:stop:step]
Indexes
When slicing, The indices are points in between the characters, not on
the characters.
+---+---+---+---+---+
| m | o | v | i | e |
+---+---+---+---+---+
0 1 2 3 4 5
-5 -4 -3 -2 -1
If slice from 0 until 2, I get ‘mo’ in the example above and not ‘mov’.
53
String Manipulation in Python - Renan Moura - [Link]
Since a string is just a list of characters, the same applies with to list:
my_list = [1, 2 , 3, 4, 5]
Becomes:
+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 |
+---+---+---+---+---+
0 1 2 3 4 5
-5 -4 -3 -2 -1
Examples
We have a variable containing the string ‘movie’ like so:
word = 'movie'
Example 1
sliced = word[:2]
print(sliced)
mo
Notice that we could have used 0 to denote the beginning, but that is
not necessary.
Example 2
sliced = word[-1]
54
String Manipulation in Python - Renan Moura - [Link]
print(sliced)
e
Example 3
sliced = word[::2]
print(sliced)
mve
Example 4
sliced = word[::-1]
print(sliced)
eivom
If you set the step to -1 you have the opposite, go back 1 character at
a time beginning at the end of the string.
55
String Manipulation in Python - Renan Moura - [Link]
my_string = "ferrari"
my_string_reversed = my_string[::-1]
print(my_string)
print(my_string_reversed)
ferrari
irarref
The slice syntax allows you to set a step, which is -1 in the example.
If you set the step to -1 you have the opposite, go back 1 character at
a time.
So you start at the position of the last character and move backwards
to the first character at position 0.
56
String Manipulation in Python - Renan Moura - [Link]
Conclusion
That’s it!
Twitter: [Link]
Linkedin: [Link]
Instagram: [Link]
57