Python String Functions – Tutorial With Examples

By Kamila

By Kamila

Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.
Updated March 7, 2024

Python String Functions Tutorial explains how easy it is to manipulate strings in Python with examples:

Strings are one of the most commonly used data types in any programming language.

It represents textual data and sequences of characters. In Python, strings are immutable and ordered. This is very important while implementing a function to operate a string. Fortunately, Python has rich in-built functions that manipulate strings.

=> Explore The Simple Python Training Series Here

In this tutorial, we shall look at how easy it is to manipulate strings in Python under a list of topics mentioned below.

String Functions, Substrings, and String Interpolation

String Functions, Substrings and String Interpolation (1)

VIDEO Tutorials: String Functions in Python

String Functions

Python has a good number of built-in functions that operate on strings. As a reminder, in Python, built-in functions are predefined functions in the programming language itself and are made readily available without importing them.

It should be noted that these functions are not specific to string only. They may be common to other sequence types(list, tuple, dict, range) and even other data types. However, in this section, we just want to demonstrate how they can be used to manipulate strings.

The table below demonstrates how some of these functions are used on strings.

NB: All examples are run on the Python shell terminal.

Table 1: Python built-in Functions that manipulate string objects

Function DescriptionExample
len(s)Returns the number of elements in a sequence.
Whitespaces are also considered as elements in the string.
len(s)
min(s[,default=obj, key=func])Returns the minimum value in a sequence.

With strings, it uses the element’s unicode code which could be obtained with ord() to do comparison.
fig1_min
max(s[,default=obj,key=func])Returns the maximum value in a sequence.
With strings, it uses the element’s unicode code to do comparison.
Just as min() above, it has a key parameter that can take a function through which each element can be manipulated before comparison.
fig1_max
type(s)Returns the type of the object.
In this case, the type of a string is of the class, str.
fig1_type
float([s])Returns a floating point number. It takes in an optional input and returns its floating point representation.
If input is string, it should be numeric else AttributeError will be raised.
fig1_float
print(s[, end=None])Prints data into the console.
If data is string, prints its characters into the console
fig1_print
int(s[,base=10])Returns an integer object of an input. The integer object is in base 10 by default.
For base 10 , the string input should be numeric else ValueError will be raised.
For other bases(2, 16, 8, etc), input strings should have a valid prefix.
fig1_int
id(s)Returns a unique and constant integer number that represents the identity of the input object during its lifetime.fig1_id
ord(s)Returns the integer Unicode code of a single-character string(string of length 1).
It is case sensitive, meaning an uppercase letter has a unicode code different from its lowercase.
By default, max() and min() converts letters to their unicode code before comparing them.
fig1_ord
ascii(s)Returns the string representation of an object, escaping all non-ASCII characters like è, õ, é with \x, \u or \Ufig1_ascii
bytearray(s[,encoding[,error]])Returns a bytearray object of the given input that contains an array of bytes. Indexing the array returns the Unicode code of the element at that index.
If input is string, the encoding should be specified.
fig1_bytearray
bytes(s[,encoding[,error]])Returns a byte object of the given string.
If input is string, the encoding should be specified
fig1_byte
enumerate(s[, start=0])Returns an enumerate object that contains each element of the object and its index position in the object into a tuple. By default, the index starts at 0, but can be changed using the start parameter.
The returned object is a generator, and we can use next() to get the next item in the object. In the example on the right, we used list() to convert the object into a list.
fig1_enumerate
hash(s)Returns a hash value for the string which stays the same during the lifetime of the program scope.fig1_hash
map(func, s)Applies a function on all elements of an iterator and returns a generator object of the results.fig1_map
slice([start,]stop[,step]])Returns a slice object that can be used to slice a stringfig1_slice

Accessing Substrings

We already saw that strings are a sequence of characters in Python, a substring is just a portion of a string.

For example, say “Hello World!” is a string, we can have the following substrings and more: “Hello”, “World”, “He”, etc. To know more about strings, please check out our tutorial on Python Strings.

Fortunately, Python provides various methods to get substrings from strings. In this section, we shall look in detail into these methods.

String Indexing

It is common for all programming languages to access items of an ordered set of data using a number representing the item’s position in that data. This process is called indexing. In Python, since strings are ordered sequences of characters, it fully supports this indexing mechanism.

We may think that as string indexing enables us to retrieve just a single character from a string, hence it can’t be referred to as substring. However, there is no representation of a single character in Python like there is in other programming languages like C. Python treats any element enclosed in single or double quotes as a string.

String indexing has been fully covered in our tutorial on Python String Operator and Methods.

String Slicing

In Python, string slicing enables us to extract substrings from a string by applying some complex manipulation of indices. This is one of the most powerful operations that sequences provide. String slicing has been fully covered in our tutorial on “Python String Operator and Methods“.

Early in this tutorial, in the Table 1 above, we saw the built-in function slice() which returns a slice object that can be used to slice a string.

Syntax

slice([start,] stop[, step])

or

slice(stop)
slice(start, stop[, step])

Given below are some details about its syntax’s parameters

  • start: Represents the starting index value for the slicing. It’s optional and it defaults to None(beginning of the string at index 0).
  • stop: Represents the ending index value for the slicing. It’s mandatory.
  • step: Represents the steps between each index for slicing. It’s optional and it may be an increment step(positive number) or decrement step(negative number).

After a slice object is created, we can apply it to a string using the index operator [ ].

Example 1: Access substring of a string from index 1 to index 15, and a step of 2.

>>>greetings = "Hello World!"   # define a string
>>> s = slice(1,15,2)    # define our slicing object
>>> greetings[s]      # apply slicing 
'el ol!'

The operation above is similar to using a basic String slicing.

>>> greetings = "Hello World!"
>>> greetings[1:15:2]
'el ol!'

We will notice that, unlike indexing, slicing doesn’t raise a string index out of range(IndexError) if the slice indexes are greater than the string’s length.

The difference between the basic slicing with [start, stop, step] and the built-in slice() function is that the latter returns an object that can be used and reused in other parts of the application. Also, it is possible to access each index of the object.

>>> s = slice(1,15,2)  # define our slice object
>>> s.start     # access the start parameter
1
>>> s.stop    # access the stop parameter
15
>>> s.step     # access the step parameter
2

Iterating Strings

One of the most common operations on sequences is to iterate over its elements. This way, we can do multiple operations on each of its elements.

In this section, we can use the for statement with a combination of Python range(), slicing operator and other few functions and methods for this purpose.

Example 2: Iterating the string characters

This way doesn’t require any extra technique order than the for loop.

site = "Software Testing Help" # define our string
# iterate over the string
for s in site:
    print(s)

Output

iterating the string characters

The for loop in this case accesses each item of the string directly, and then we use the built-in print() function seen in Figure 1 to print out the characters to the console.

Example 3: Iterating the string characters by their indexes

Here, we use a combination of a for loop, indexing and the built-in range() and len() functions seen in Figure 1.

site = "Software Testing Help" # define our string

# iterate the string using for loop, indexing and the range and len 
L = len(site) # get string length 
for idx in range(L): # loop through a range from 0 to L(string length)
    char = site[idx] # get each element using the indexing operator
    print(char)

Range_len

Here, our range() function takes in the length of our string, L. Then creates a range object range(0, L-1, 1) where 0 and 1 are defaults for start and step respectively, which is then iterated over with the for loop.

NB: The built-in range() function wasn’t introduced in table 1 above because it doesn’t operate on strings directly. To know more about this function, check the tutorial on Python Range.

Iterating over the range object will provide us with indexes that we can use with the indexing operator to get the string’s elements.

Example 4: Iterating the string’s characters and indexes

By using the built-in enumerate() function seen in the table 1 above, we can iterate a string and in each iteration, obtain a character of the string and its index.

site = "Software Testing Help" # define our string

# Iterate the string using the enumerate()
for idx, char in enumerate(site):
    print("Index: ", idx, "Char: ", char)

Output

Iterating the string’s characters and indexes

Example 5: Iterating a substring of a string

With the help of the slice operator seen in the Table 1 above, we can extract a substring from a string. This operator gives us the possibility to extract substrings in more complex ways.

Example 5.1: Iterating the first 8 characters of a string.

site = "Software Testing Help" # define our string

# iterate the first 8 characters of the string using slice operator
for char in site[0:8]:
    print(char)

Output

Iterating the first 8 characters of a string

Example 5.2: Iterating a string backward

Using the slice operator, we can iterate a string or substring of a string in reverse order. Of course this can be achieved by using a negative step value.

site = "Software Testing Help" # define our string

# iterate the string in reversed order using slice operator
for char in site[::-1]:
    print(char)

Output

Iterating a string backward

Operating on a Substring

In Natural language processing which is a subfield of linguistic, computer science and artificial intelligence, operating on text is the key. Some of the operations are: verifying if a word exists in a text, counting the number of unique words in a text, getting all positions of a certain word in a text and many more.

In this section, we shall cover just a tip of the iceberg of what is required in NLP.

Verify if Substring Exists

In the most basic ways, we can verify if a substring is present in a string with either the in operator or the String find() method.

Example 6: Using the in operator

>>> site = "Software Testing Help" # define our string
>>> sub = "Help"  # first substring
>>> sub2 = "money" # second substring
>>> sub in site # check if substring(sub) exist in string(site), True
True
>>> sub2 in site # check if substring(sub2) exist in string(site), False
False

Example 7: Using the String find() method

>>> site = "Software Testing Help" # define our string
>>> sub = "Help" # define a substring
>>> sub2 = "money" # define a substring
>>> site.find(sub)# check if substring exist, returns index position
17
>>> site.find(sub2) # check if substring exist, returns -1 for not present
-1

Looking at the example 6 and 7 above, we see that both the methods successfully verify if a substring is present in a string. However, their outputs are different. The in operator returns a boolean value while find() returns the index of the substring in the string or -1 if not present.

NB: Strings also define the method rfind() which is similar to find() except that the search for find() is from left to right while that of rfind() is from right to left. It is important to know this difference because they both return the index of the first occurrence of the substring.

Count Substring Number of Occurrences

The most common way to find the number of occurrences of a substring in a string is by using the String count() method.

It returns 0 if the substring is not present in the string. This makes sense as this means that the occurrence is equivalent to zero or nothing.

Example 8:

>>> s = "This is The Best Website Ever"
>>> s.count('Th')
2
>>> s.count('a')
0

Modifying Strings

Having such a data type as strings, you may reach an instance where you want to change or modify a particular variable to perhaps change a character at a particular location, remove a character or maybe just add one or a few.

Coming from another programming language like C, you may assume the tendency to just do it using the string indexing as below.

String[0] = "k"

Though very intuitive, this does not work with python, because strings are a part of the data types that are immutable, and once created, cannot be modified.

Plus, the assignment operator (=) is not supported by strings.

Example 9: Modify a string using the assignment operator

>>> s = "Hello World!" # define string
>>> s[0] = "h" # assign by string indexing
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

Which just says in other words that strings are immutable and so we cannot change the character at that point through assignment.

So to modify a string, you have to create a new copy, modify it and then replace the old string with the new one, we can use one of the string replace() methods and the concatenation operator.

Example 10: Using the string replace() method

>>> s = "hello World"
>>> newS = s.replace('h','H') # replace 'h' with 'H'
>>> print(newS) # new string is created
Hello World
>>> print(s) # original string remains unchanged.
hello World

Example 11: Using concatenation operator(+)

>>> s = "Hello World"
>>> newS = s + "!" # concatenate '!' to the original string
>>> print(newS) # new string is created
Hello World!
>>> print(s) # original string remains unchanged
hello World

Interpolating Variables into Strings

This is a process of substituting and placing values of a variable in certain points in a string. This process is used to insert specified value(s) into a string at specific points, whereby these points can be specified depending on the chosen method of approach which we shall see soon.

The importance of interpolating variables into strings is that, in as much as you can possibly achieve the same output of interpolating strings with concatenation. Concatenation is clumsy and you need the readability and flexibility that interpolation provides.

Prior to Python 3.6, we had two ways of interpolation: %-formatting and str.format(). However, as from Python v3.6, the f-string was introduced and it provides a better and more prominent way to interpolate.

Let’s take a look at the ways to achieve this interpolation.

#1) Using %-Formatting

We start off with the oldest formatting that has existed since the very beginning. Though old, it is not recommended nowadays.

Syntax

It uses the strings built-in % operator to insert values at specific positions in the string. It requires at least two % operators, one in the string itself acting as a placeholder for the value and one separating the string from the value that needs to be inserted into the string.

Example 12: A basic way of formatting with %-formatting

>>> site = "Software Testing Help"
>>> print("You are reading this article at %s" % site)
You are reading this article at Software Testing Help

In order to insert multiple variables into the string, a tuple must be used to hold these variables.

Example 13: Insert multiple variables into a string using %-formatting

>>>name = "Eyong Kevin"
>>> age = 30
>>> print("name: %s, age: %s" % (name, age))
name: Eyong Kevin, age: 30

As we see from the above, we use the %s to specify the specific location where we want the string to be inserted and before specifying the tuple that holds the values, we precede it with the % operator.

In Python 2.6, the string format() method was introduced to replace this method. Please, check the next section below.

#2) Using String Format() Method

The format method is one of the most versatile ways of interpolating strings introduced in Python 2.6 to improve on the %-formatting method.

Syntax

string.format(var1, var2,...)

This method works by inserting into the string one or more placeholders defined by a pair of curly braces {}, then the number of data or variables corresponding to the number of placeholders that are passed as parameters into the format method.

A new string will be returned where the placeholder curly braces are replaced by the format() parameters.

NB: The number of arguments given to format() should match the number of curly braces in the string. It will raise an IndexError exception if the number of curly braces are more than the arguments.

Example 14:

name = "Eyong Kevin"
age = 30

print("Hello! My names are {} and I am {} years old".format(name, age))
print("Hello! My names are {n} and I am {a} years old".format(n=name, a=age))
print("Hello! My names are {1} and I am {0} years old".format(age, name))

Output

format()

As we can see above, these are the three different ways of interpolating strings with the format() method, where the last two ways actually remove the constraint of positioning the values passed to the format() method in the order they should appear but rather use the associated name and index respectively.

As of Python 3.6, f-string was introduced to overcome some of the limitations that the string format() method had.

#3) Using f-String

This is a means of formatting that is achieved by appending the prefix in front of the string you want to interpolate with ‘f’. It is very similar to the format() method in the way in which it specifies the location in which you want to insert the variable or expression.

This is probably the most versatile form of interpolation because unlike the format() you can proceed to specify the variables and or expressions right at the point of the curly braces. This makes it less verbose and error-prone when compared to both format() and %-formatting.

Syntax

Python f-strings start with the letter f followed by a string with curly braces which contains expressions or variables that will be replaced by their values.

Example 15:

>>> name = "Eyong Kevin"
>>> f'My name is {name}'
'My name is Eyong Kevin'

As explained above, the f-strings formatting also permits the evaluation of expressions. This means that if we place an expression inside the curly braces, then f tells python to evaluate the expression and the result is inserted to the string at that point.

>>> f'The sum of 3 and 6 is {3 + 6}'
'The sum of 3 and 6 is 9'

Frequently Asked Questions

Q #1) What does %d do in Python?

Answer: Unlike %s which is used as a placeholder in %-formatting to inject a string value into a string, %d is used as a placeholder for numeric values.

>>> "Hi, I am %d years old" % 30
'Hi, I am 30 years old'

Q #2) What does F mean in Python?

Answer: In Python 3.6, a new way of string interpolation called “formatting string literal” or “f-strings” was introduced. The letter f is a part of its syntax where it starts off with f, followed by a string within which curly braces are used to contain expressions or variables that will be replaced by their values.

>>> age = 30
>>> f"Hi, I am {age} years old"
'Hi, I am 30 years old'

Q #3) Does Python have a data type for a single character?

Answer: Python doesn’t define a data type for a single character. Unlike C which defines the char data type to hold a single character, Python considers everything enclosed in double or single quotes as strings. Mostly we only refer to a single character in Python when a string has a length of 1.

Q #4) What are substrings in Python?

Answer: In Python, substrings are a sequence of characters or character(string of length 1) within another string. We can find out if a substring belongs to a string using the find() method and in operator.

Q #5) What does .2f mean in Python?

Answer: In Python, the %-formatting uses %s, %d and %f as placeholders for strings, numeric values and floating point numbers respectively. We can specify how many decimal places to represent with %.nf where n stands for the number of decimal places. Hence, for 2 decimal places, we use %.2f

>>> num = 45.3348574
>>> "my float is: %.2f" % num
'my float is: 45.33'

Q #6) Can we modify Python strings?

Answer: Python strings are immutable. Meaning once created, they can’t be changed or modified in place. That’s why all String’s methods and functions that operate on a string return a new string containing any modifications made but the original string always stays the same.

More About Python String Functions

Let’s take a look at the various string functions in Python.

#1) String Replace

This method is used to replace the string, which accepts two arguments.

Example:

lang = “Hello Selenium”
print(lang.replace(“Selenium”, “Python”)) 

Output:

Hello Python

Python - string_replace

Output:

Python string - output

#2) String Reverse

This method is used to reverse a given string

Example:

 
lang = “Python”
print(‘’.join(reversed(lang))) 

Output:

nohtyP

Python string reverse

Output:

Python string reverse output

#3) String Join

This method returns the string concatenated with the elements of iterable.

Example:

 
s1 = “ABC”
s2 = “123”
print(s1.join(s2)) 

Output:

1ABC2ABC3

Python string join

Output:

Python string join output

#4) String Split

This method is used to split the string based on the user arguments

Example:

 
text = “Welcome to Python”
print(text.split()) 

Output:

[‘Welcome’, ‘to’, ‘Python’]

Python string split

Output:

string_split_output

#5) String Length

This method returns the length of the String.

Example:

 
text = “Python”
print(len(text)) 

Output:

6

Python string length

Output:

Python string length output

#6) String Compare

This method is used to compare two strings.

Example:

 
s1 = “Python”
s2 = “Python”
if(s1 == s2):
      print(“Both strings are equal”) 

Output:

Both strings are equal

Python string compare

Output:

Python string compare output

#7) String Lowercase

This method is used to convert the uppercase to lowercase.

Example:

text = “PYTHON”
print(text.lower()) 

Output:

python

Python string lowercase

Output:

Python string lowercase output

Conclusion

In this tutorial, we looked at the various Python built-in functions that operate on strings like ord, enumerate, max, etc. We also looked at substrings, what they are and how to operate on them.

Lastly, we discussed string interpolation from the oldest which is %-formatting to the newest which is f-strings.

=> Read Through The Easy Python Training Series

Was this helpful?

Thanks for your feedback!

Leave a Comment