As the name suggests, Python 3 string functions are designed to perform various operations on strings. There are several dozen string functions in the Python programming language. In this article, we will cover the most commonly used ones and several special functions that may be less popular but are still useful. They can be helpful not only for formatting but also for data validation.
First, let’s discuss string formatting functions, and to make the learning process more enjoyable, we will use texts generated by a neural network in our examples.
capitalize()
— Converts the first character of the string to uppercase, while all other characters will be in lowercase:
>>> phrase = 'the shortage of programmers increases the significance of DevOps. After the presentation, developers start offering their services one after another, competing with each other for DevOps.'
>>> phrase.capitalize()
'The shortage of programmers increases the significance of devops. after the presentation, developers start offering their services one after another, competing with each other for devops.'
casefold()
— Returns all elements of the string in lowercase:
>>> phrase = 'Cloud providers offer scalable computing resources and services over the internet, enabling businesses to innovate quickly. They support various applications, from storage to machine learning, while ensuring reliability and security.'
>>> phrase.casefold()
'cloud providers offer scalable computing resources and services over the internet, enabling businesses to innovate quickly. they support various applications, from storage to machine learning, while ensuring reliability and security.'
center()
— This method allows you to center-align strings:
>>> text = 'Python is great for writing AI'
>>> newtext = text.center(40, '*')
>>> print(newtext)
*****Python is great for writing AI*****
A small explanation: The center()
function has two arguments: the first (length of the string for centering) is mandatory, while the second (filler) is optional. In the operation above, we used both. Our string consists of 30 characters, so the remaining 10 were filled with asterisks. If the second attribute were omitted, spaces would fill the gaps instead.
upper()
and lower()
— convert all characters to uppercase and lowercase, respectively:
>>> text = 'Projects using Internet of Things technology are becoming increasingly popular in Europe.'
>>> text.lower()
'projects using internet of things technology are becoming increasingly popular in europe.'
>>> text.upper()
'PROJECTS USING INTERNET OF THINGS TECHNOLOGY ARE BECOMING INCREASINGLY POPULAR IN EUROPE.'
replace()
— is used to replace a part of the string with another element:
>>> text.replace('Europe', 'USA')
'Projects using Internet of Things technology are becoming increasingly popular in the USA.'
The replace()
function also has an optional count attribute that specifies the maximum number of replacements if the element to be replaced occurs multiple times in the text. It is specified in the third position:
>>> text = 'hooray hooray hooray'
>>> text.replace('hooray', 'hip', 2)
'hip hip hooray'
strip()
— removes identical characters from the edges of a string:
>>> text = 'ole ole ole'
>>> text.strip('ole')
'ole'
If there are no symmetrical values, it will remove what is found on the left or right. If the specified characters are absent, the output will remain unchanged:
>>> text.strip('ol')
'e ole ole'
>>> text.strip('le')
'ole ole o'
>>> text.strip('ura')
'ole ole ole'
title()
— creates titles, capitalizing each word:
>>> texttitle = 'The 5G revolution: transforming connectivity. How next-gen networks are shaping our digital future'
>>> texttitle.title()
'The 5G Revolution: Transforming Connectivity. How Next-Gen Networks Are Shaping Our Digital Future'
expandtabs()
— changes tabs in the text, which helps with formatting:
>>> clublist = 'Milan\tReal\tBayern\tArsenal'
>>> print(clublist)
Milan Real Bayern Arsenal
>>> clublist.expandtabs(1)
'Milan Real Bayern Arsenal'
>>> clublist.expandtabs(5)
'Milan Real Bayern Arsenal'
Sometimes, it is necessary to count a certain number of elements in a sequence or check if a specific value appears in the text. The following string functions solve these and other tasks.
count()
— counts substrings (individual elements) that occur in a string. Let's refer again to our neural network example:
>>> text = "Cloud technologies significantly accelerate work with neural networks and AI. These technologies are especially important for employees of large corporations operating in any field — from piloting spacecraft to training programmers."
>>> element = "o"
>>> number = text.count(element)
>>> print("The letter 'o' appears in the text", number, "time(s).")
The letter 'o' appears in the text 19 time(s).
As a substring, you can specify a sequence of characters (we'll use text from the example above):
>>> element = "ob"
>>> number = text.count(element)
>>> print("The combination 'ob' appears in the text", number, "time(s).")
The combination 'in' appears in the text 5 time(s).
Additionally, the count()
function has two optional numerical attributes that specify the search boundaries for the specified element:
>>> element = "o"
>>> number = text.count(element, 20, 80)
>>> print("The letter 'o' appears in the specified text fragment", number, "time(s).")
The letter 'o' appears in the specified text fragment 6 time(s).
The letter 'o' appears in the specified text fragment 6 time(s).
find()
— searches for the specified value in the string and returns the smallest index. Again, we will use the example above:
>>> print(text.find(element))
7
This output means that the first found letter o
is located at position 7 in the string (actually at position 8, because counting in Python starts from zero). Note that the interpreter ignored the capital letter O
, which is located at position zero.
Now let's combine the two functions we've learned in one code:
>>> text = "Cloud technologies significantly accelerate work with neural networks and AI. These technologies are especially important for employees of large corporations operating in any field — from piloting spacecraft to training programmers."
>>> element = "o"
>>> number = text.count(element, 20, 80)
>>> print("The letter 'o' appears in the specified text fragment", number, "time(s), and the first time in the whole text at", (text.find(element)), "position.")
The letter 'o' appears in the specified text fragment 3 time(s), and the first time in the whole text at 7 position.
index()
— works similarly to find()
, but will raise an error if the specified value is absent:
Traceback (most recent call last):
File "C:\Python\text.py", line 4, in <module>
print(text.index(element))
ValueError: substring not found
Here's what the interpreter would return when using the find()
function in this case:
-1
This negative position indicates that the value was not found.
enumerate()
— a very useful function that not only iterates through the elements of a list or tuple, returning their values, but also returns the ordinal number of each element:
team_scores = [78, 74, 56, 53, 49, 47, 44]
for number, score in enumerate(team_scores, 1):
print(str(number) + '-th team scored ' + str(score) + ' points.')
To output the values with their ordinal numbers, we introduced a few variables: number
for ordinal numbers, score
for the values of the list, and str
indicates a string. And here’s the output:
1-th team scored 78 points.
2-th team scored 74 points.
3-th team scored 56 points.
4-th team scored 53 points.
5-th team scored 49 points.
6-th team scored 47 points.
7-th team scored 44 points.
Note that the second attribute of the enumerate()
function is the number 1, otherwise Python would start counting from zero.
len()
— counts the length of an object, i.e., the number of elements that make up a particular sequence:
>>> len(team_scores)
7
This way, we counted the number of elements in the list from the example above. Now let's ask the neural network to write a string again and count the number of characters in it:
>>> network = 'It is said that artificial intelligence excludes the human factor. But do not forget that the human factor is still present in the media and government structures.'
>>> len(network)
162
join()
— allows you to convert lists into strings:
>>> cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Philadelphia', 'San Antonio']
>>> cities_str = ', '.join(cities)
>>> print('Cities in one line:', cities_str)
Cities in one line: New York, Los Angeles, Chicago, Houston, Phoenix, Philadelphia, San Antonio
print()
— provides a printed representation of any object in Python:
>>> cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Philadelphia', 'San Antonio']
>>> print(cities)
['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix', 'Philadelphia', 'San Antonio']
type()
— returns the type of the object:
>>> type(cities)
<class 'list'>
We found out that the object from the previous example is a list. This is useful for beginners, as they may initially confuse lists with tuples, which have different functionalities and are handled differently by the interpreter.
map()
— is a fairly efficient replacement for a for loop, allowing you to iterate over the elements of an iterable object, applying a built-in function to each of them. For example, let's convert a list of string values into integers using the int function:
>>> numbers_list = ['4', '7', '11', '12', '17']
>>> list(map(int, numbers_list))
[4, 7, 11, 12, 17]
As we can see, we used the list()
function, "wrapping" the map()
function in it—this was necessary to avoid the following output:
>>> numbers_list = ['4', '7', '11', '12', '17']
>>> map(int, numbers_list)
<map object at 0x0000000002E272B0>
This is not an error; it simply produces the ID of the object, and the program will continue to run. However, the list()
method is useful in such cases to get the desired list output.
Of course, we haven't covered all string functions in Python. Still, this set will already help you perform a large number of operations with strings and carry out various transformations (programmatic and mathematical).