In this article, you will see some of the most commonly used Python string functions.
Strings in Python store data in the form of a sequence of characters. There are a lot of default Python string functions that can be used to manipulate strings in Python. In this article, you will see some of the most commonly used Python string functions.
Let’s first see how to create a string in Python.
Creating Strings in Python
To create Python strings, all you have to do is wrap some text inside single or double quotes as shown below:
str1 = "Codezen-dot-io" print(type(str1))
The above script creates a simple string
Codezen-dot-io and stores it in the variable
str1. Next, the
type() function prints the type of the
str1 variable. Here is the output:
Since a string is simply a sequence of characters, we can access string characters via indexes as shown below:
str1 = "Codezen-dot-io" print(str1) ## prints first character print(str1[-1]) ## prints last character
The above script prints the first and last characters of the string
Codezen-dot-io as shown by the following output:
Common Python String Functions
Some of the most commonly used Python string functions are as follows:
1. len() – Find string length
len() function in Python returns the length of a string. It counts the characters in a string (including spaces) and returns an integer value. Here is an example.
str1 = "Codezen-dot-io" print(len(str1))
2. Split() – Split a string
split() function splits or breaks a string whenever a specific character or substring occurs. The character or substring used to break a string is passed as a parameter to the
split() function. For instance, the following
split() function splits the string at the locations where a dash “-” occurs. The split() function returns a list of split strings.
str1 = "Codezen-dot-io" str_split = str1.split("-") print(type(str_split)) print(str_split)
Here is the output:
<class 'list'> ['Codezen', 'dot', 'io']
3. String concatenation
There are two ways to concatenate a string. You can simply join strings using the addition operator “+” or you can use the
join() function to concatenate multiple strings. With the
join() function, you need to pass the separator that will be inserted between the individual strings. Look at the following script for example. Here the join() function joins multiple strings by inserting dashes “-” as individual string separators.
str1 = "codezen" str2 = "dot" str3 = "io" concat = str1 +"-" +str2 +"-"+str3 print(type(concat)) print(concat) concat1 = "-".join((str1,str2,str3)) print(type(concat1)) print(concat1)
The output of the above string is as follows:
<class 'str'> codezen-dot-io <class 'str'> codezen-dot-io
4. Finding the start and end of a string
You can also check if a string starts or ends with a particular substring or not. To do so, you can use the
endswith() functions, respectively. Here is an example.
str1 = "Codezen-dot-io" print(str1.startswith("Co")) str1 = "Codezen-dot-io" print(str1.endswith("Co"))
5. Converting a string to lower or upper case
You can convert a string into a full lower or full upper case via
upper() functions, respectively. The following script demonstrates a simple example of converting a string to lower and upper cases.
str1 = "Codezen-dot-io" print(str1.lower()) str1 = "Codezen-dot-io" print(str1.upper())
Here is the output:
6. Swapping string case
In addition to converting a string to a full lower or full upper case, you can also swap its case. You can use the
swapcase() function to do so. By swapping a string’s case, you convert all the uppercase characters in a string to lowercase, and vice versa. Here is an example.
str1 = "Codezen-dot-io" print(str1.swapcase())
The following output shows that “c” at the start of the string “Codezen-dot-io” was initially uppercase, but now it has been converted to the lowercase. The remaining characters were initially lower case and hence have been converted to upper case.
7. Reversing a string
A string can be reversed by passing string contents to the
reversed() function. The
reversed() function returns a reversed string object. You need to call the
join() function on the reversed string object to reconstruct the reversed string. Look at the following example to understand this concept.:
str1 = "Codezen-dot-io" print("".join(reversed(str1)))
In the output you can see the reversed string:
8. Checking if a string is numeric
To check if a string contains all numbers, you can use the
isnumeric() function. The function returns true if a string is made up of numbers, else the
isnumeric() function returns false.
str1 = "446454" print(str1.isnumeric()) str2 = "446454a" print(str2.isnumeric())
9. Converting a string to Integer or Float
You can convert a string to integer or floating data type via the
float() functions, respectively. Only numeric strings can be converted into floats or integers, therefore it is always better to first check if a string is numeric via the
isnumeric() function before passing the string to the
int() functions. The script below contains a simple example of
int() string functions.
int1 = "24621" print(int1) print(type(int(int1))) flt1 = "24.621" print(flt1) print(type(float(flt1)))
24621 <class 'float'> 24.621 <class 'float'>
10. Checking if a string contains a substring
To check if a string contains a substring, you can use membership operators. The
in membership operator returns true if a string contains a particular substring while the
not in operator returns true in the reverse case. Here is an example.
str1 = "Codezen-dot-io" print("zen" in str1) str1 = "Codezen-dot-io" print("nez" in str1)
Here is the output of the above string:
The string data type in Python is used to store textual data. This article explains some of the most commonly used Python string functions to convert a string to upper and lower cases, split a string, find the length of a string, check a string for the occurrence of a particular substring, and join strings.
To study more about Python string functions, check the official python documentation for common string operations.