Python Strings - Overview of the Basic String Operations

Strings are one of the basic data types in Python. Python strings are a combination of any number of characters made of letters, digits, and other special characters. In this tutorial, you will learn how to create, manipulate, and format them to use under different scenarios.

Create New Strings in Python

To create a new Python string, you just have to declare a sequence of characters enclosed by single or double quotation marks. Triple quotation marks are also used for multiple-lines-long strings.

double_quotes = "My name is John!"

single_quotes = 'My name is John!'

multi_line_string = '''1. My name is John!
                       2. I am a programmer'''

String Indexing

Every character in a Python string has an integer index. The indexing starts from 0 at the first character and increment along the string. You can use the index of an individual character to retrieve that character from the string like the following example shows.

myPet = "Dog not a cat"

myPet[0]  # 'D'
myPet[5]  # 'o'
myPet[7]  # ' '
myPet[12] # 't'
# myPet[15] # IndexError

Trying to access a character beyond the index of the final character results in an IndexError.

You can access a character in a string using a negative index. In this case, the indexing starts from -1 at the final character of the string, and negatively increase as you go backward.

myPet = "Dog not a cat"

myPet[-1] # 't'
myPet[-6] # ' '
myPet[-8] # 'o'
myPet[-13] # 'D'

String Slicing

Slicing is the method of extracting a substring (a part of the string) from a string. This task is achieved with the help of string indexing.

myPet = "Dog not a cat"

myPet[5:7] # 'ot'
myPet[1:12] # 'og not a ca'

Here, two indices are provided separated by a colon, the first index indicates where to start slicing and the second index indicates where to stop. The resulting substring includes characters from the starting index to the character before the ending index, the character at the ending index is not included in the substring.

If you don’t provide the starting index, slicing begins at the first character of the string. If you don’t provide the ending index, slicing ends at the final character while including it in the resulting substring.

myPet = "Dog not a cat"

myPet[:7] # 'Dog not'
myPet[10:] # 'cat'
myPet[:] # 'Dog not a cat'

You can provide negative indices as slicing indices as well.

myPet = "Dog not a cat"

myPet[10:-1] # 'ca'

Length of a String

The built-in Python method len() outputs the length of a string.

myPet = "Dog not a cat"
len(myPet) # 13

Iterate through a String

You can iterate through each character in a string using a for loop.

Example:

name = "John"
for char in name:
    print(char)
# 'J', 'o', 'h', 'n'

String Concatenation

String concatenation is the joining of two or more strings to create a single string. In Python, there are several methods to concatenate strings.

One is using the + operator.

str1 = 'Hello'
str2 = 'World'
concat_str = str1 + str2 # 'HelloWorld'
concat_str = str1 + ' ' + str2 # 'Hello World'

You can use the * operator to concatenate a string to itself any number of times.

concat_str = str1*3 # 'HelloHelloHello'

Another way to concatenate strings is via the join() method.

The built-in join() method is used to concatenate an array of strings using a common separator.

arr = [str1, str2]
concat_str = (' ').join(arr) # 'Hello World'
concat_str = (',').join(arr) # 'Hello,World'

In the above code, the first join() method adds a white space between every word in the array.

The second join() method inserts a comma between every word in the array.

String and Int Concatenation

In Python, we can also concatenate a string to an integer but not with the + operator. If we attempt to use the following code:

name = "John"
age = 35

print(a + b)

We would get:

Traceback (most recent call last):
  File "concat.py", line 5, in <module>
    print(a + b)
TypeError: can only concatenate str (not "int") to str

To avoid this error, we can use the str() method to convert the integer to string, for example:

name = "John "
age = "35"

print(a + str(b)) #John 35

How to Split a String

The built-in split() method is used to split a single string into an array of strings.

string = "My name is John"
split_arr = string.split(' ') # ['My', 'name', 'is', 'John']

We can also split a string using a separator:

string = "John, Rose, Jack, Mary"
split_arr = string.split(', ') # ['John', 'Rose', 'Jack', 'Mary']

Strip - Remove White Spaces

strip(), the built-in string method is used to remove the white spaces from the beginning and end of a string.

string = "    Hello,    World    "
stripper_str = string.strip() # 'Hello,    World'

As you can see, strip() does not remove the white spaces that are in between other characters but only at the two ends.

There are two variants of the strip() method, Left Strip and Right Strip:

  • lstrip()
  • rstrip()

These methods remove white spaces at the left side and right side of the string, respectively.

Example:

lsplit_str = string.lstrip() # 'Hello,    World    '
rsplit_str = string.rstrip() # '    Hello,    World'

Strip methods are especially useful when reading user inputs, where extra white spaces could be passed by the users.

Formatting a String

Python’s format() method is used to format a string. Curly braces {} are used inside the string that needs to be formatted as a placeholder for the part that needs to be replaced by the arguments provided to the format() method.

Example:

"Hello, {}".format('John') # 'Hello, John'

In the above example {} is replaced by ‘John’ in the formatted string.

You can use more than one curly braces inside the string to format. They are replaced by the arguments provided to format() method either in the provided order (if no positional indices are mentioned inside curly braces) or positional order.

Example:

"I have a {}, {}, and a {}".format('dog', 'cat', 'rabbit') # 'I have a dog, cat, and a rabbit'
"I have a {1}, {0}, and a {2}".format('dog', 'cat', 'rabbit') # 'I have a cat, dog, and a rabbit'

Instead of using indices, you can provide keyword arguments to the format() method so that those keywords can be used inside curly braces.

Example:

print("{friend} is my friend and {enemy} is my enemy".format(friend="John", enemy="Jack"))
# 'John is my friend and Jack is my enemy'

The format() method is quite versatile as it can be used for many use cases.

Here are some other applications of the format() method:

arr = [3, 5]
'I have {0[0]} dogs and {0[1]} cats'.format(arr)
# 'I have 3 dogs and 4 cats'

#convert numbers to different bases
"int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42)
# 'int: 42;  hex: 2a;  oct: 52;  bin: 101010'

Convert a String to Lowercase

Using Python’s lower() method, you can convert a string to lowercase.

Example:

string = "Hello, World!"
string.lower() # 'hello, world!'

Convert a String to Uppercase

Likewise, using Python’s upper() method, you can convert a string to uppercase.

Example:

string = "Hello, World!"
string.upper() # 'HELLO, WORLD!'

Conclusion

I hope that with the help of this tutorial, you are now familiar with Python strings and how to use various methods for string operations.

Reference: Python’s string documentation