Hello readers, in this tutorial, you will learn how to use Python strings, what are the built-in methods to manipulate strings in Python, and how to use them with the help of examples, Moreover, the tutorial will cover the errors or exceptions if your code is not using strings properly.
You would agree to the fact that each one of us uses Python strings very frequently in regular programming activities. Considering this, we’ve tried to cover all the Python string operators and functions under this Python string tutorial. Let’s begin now.
Learn the Basics of Python Strings
1. How to Initialize Strings in Python?
In general, a string is a sequence of alphanumeric characters or letters. You can easily initialize a string in Python by enclosing it either in a single or double quote or can even use triple quotes.
The programmers use triple quotes to define multi-line strings or docstrings. In the following example, we are providing all the available ways to initialize a string in Python.
# Below are different ways to initialize a string with same value # str_v1 = 'Python' str_v2 = "Python" str_v3 = """Python""" if str_v1 == str_v2 == str_v3: print("All three strings are equal.") # Using triple quotes, Strings can extend to multiple lines str_v4 = """ This document will help you to explore all the concepts of Python Strings!!! """ # Replacing a substrng "document" with another string "tutorial" substr_v1 = String_var.replace("document", "tutorial") print (substr_var)
2. Index and Slice Strings in Python
Access Individual Characters of a String
You need to know the index of a character to retrieve it from the string.
Like most programming languages, Python allows indexing from the zeroth position in strings. But it also supports negative indexes. The index of ‘-1’ represents the last character of the string. Similarly, using ‘-2’, we can access the penultimate element of the string and so on.
sample_str = 'Python String' print (sample_str[0]) # return 1st character # output: P print (sample_str[-1]) # return last character # output: g print (sample_str[-2]) # return last second character # output: n
Slice a String in Python
Python slicing operator (colon sign) cuts or slices a string into substrings. With the slicing operator, you can define the range as [a:b]. It returns all the characters of the string starting from index ‘a’ up to char at index ‘b-1’. So, the char at index ‘b’ is not a part of the output.
Please note that the indexes in Python begin from 0, i.e., the first element stores at the 0th position.
sample_str = 'Python String' print (sample_str[3:5]) #return a range of character # ho print (sample_str[7:]) # return all characters from index 7 # String print (sample_str[:6]) # return all characters before index 6 # Python print (sample_str[7:-4]) # St
Next, we have a no. of Python tutorials/quizzes/interview questions on this blog. If you like to try them, then refer to any of the posts listed below.
Python Strings – Common Error Codes
1- If your code accesses an out-of-range index, then Python will throw an ‘IndexError’ exception.
sample_str = "Python Supports Machine Learning." print (sample_str[1024]) #index must be in range # IndexError: string index out of range
2- The index must be of the integer data type. You should not use a float or any other data type for this purpose. Otherwise, the Python subsystem will flag a TypeError exception as it detects a data type violation for the string index.
sample_str = "Welcome post" print (sample_str[1.25]) #index must be an integer # TypeError: string indices must be integers
3. Modify/Delete a String in Python
Python strings are by design immutable. It suggests that once a string binds to a variable, it can’t be modified.
If you want to update the string, then re-assign a new string value to the same variable.
sample_str = 'Python String' sample_str[2] = 'a' # TypeError: 'str' object does not support item assignment sample_str = 'Programming String' print (sample_str) # Output=> Programming String
Similarly, we cannot modify the string by deleting some characters from it. Instead, we can remove the strings altogether by using the ‘del’ command.
sample_str = "Python is the best scripting language." del sample_str[1] # TypeError: 'str' object doesn't support item deletion del sample_str print (sample_str) # NameError: name 'sample_str' is not defined
4. String Operators in Python
Concatenation (+)
It combines two strings into one.
# example var1 = 'Python' var2 = 'String' print (var1+var2) # PythonString
Repetition (*)
This operator creates a new string by repeating it a given number of times.
# example var1 = 'Python' print (var1*3) # PythonPythonPython
Slicing [ ]
The slice operator prints the character at a given index.
# example var1 = 'Python' print (var1[2]) # t
Range Slicing [x:y]
It prints the characters present in the given range.
# example var1 = 'Python' print (var1[2:5]) # tho
Membership (in)
This operator returns a ‘True’ value if the character is present in the given string.
# example var1 = 'Python' print ('n' in var1) # True
Membership (not in)
It returns a ‘True’ value if the character is not present in the given string.
# example var1 = 'Python' print ('N' not in var1) # True
Iterating (for)
With this operator, we can iterate through all the characters of a string.
# example for var in var1: print (var, end ="") # Python
Raw String (r/R)
We can use it to ignore the actual meaning of Escape characters inside a string. For this, we add ‘r’ or ‘R’ in front of the string.
# example print (r'\n') # \n print (R'\n') # \n
5. Formatting Operators
Python Escape Characters
An Escape sequence starts with a backslash (\), which signals the compiler to treat it differently. Python subsystem automatically interprets an escape sequence irrespective of whether it is in a single-quoted or double-quoted string.
For example, one of the important escape sequences is to escape a single quote or a double quote.
Suppose we have a string like – Python is a “widely” used language.
The double-quote in the above statement disguises Python to assume that the string ends up there.
We need a way to tell Python that the double quotes inside the string are not the string markup quotes. Instead, they are part of the string and should appear in the output.
To resolve this issue, we can escape the double-quotes and single-quotes as:
print ("Python is a "widely" used language") # SyntaxError: invalid syntax # After escaping with double-quotes print ("Python is a \"widely\" used language") # Output: Python is a "widely" used language
List of Escape Characters
Here is the complete list of escape characters that are represented using backslash notation.
Escape Char | Name |
---|---|
\\ | Backslash (\) |
\” | Double-quote (“) |
\a | ASCII bel (BEL) |
\b | ASCII backspace (BS) |
\cx or \Cx | Control-x |
\f | ASCII Form feed (FF) |
\n | ASCII linefeed (LF) |
\N{name} | A character named 'name' in the Unicode-only database |
\r | Carriage Return (CR) |
\t | Horizontal Tab (TAB) |
\uyyyy | A character with 16-bit hex value yyyy (Unicode only) |
\Uyyyyyyyy | A character with a 16-bit hex value yyyy (Unicode only) |
\v | ASCII vertical tab (VT) |
\ooo | Characters with octal value ooo |
\xnn | A character with a hex value nn where n can be anything from the range 0-9 , a-f , or A-F . |
Python Format Characters
The string ‘%’ operator can be used for formatting strings. We often use this operator with the print() function.
Here’s a simple example.
print ("Employee Name: %s,\nEmployee Age:%d" % ('Ashish',25)) # Employee Name: Ashish, # Employee Age: 25
List of Format Symbols
Following is the table containing the complete list of symbols that you can use with the ‘%’ operator.
Symbol | Conversion |
---|---|
%c | character |
%s | string conversion via str() before formatting |
%i | signed decimal integer |
%d | signed decimal integer |
%u | unsigned decimal integer |
%o | octal integer |
%x | hexadecimal integer (lowercase letters) |
%X | hexadecimal integer (UPPER-case letters) |
%e | exponential notation (with lowercase ‘e’) |
%E | exponential notation (with UPPER-case ‘E’) |
%f | floating-point real number |
%g | the shorter of %f and %e |
%G | the shorter of %f and %E |
6. Unicode Support
Regular strings in Python get stored as the 8-bit ASCII value, whereas Unicode string follows the 16-bit ASCII standard. This extension allows the strings to include characters from the different languages of the world. In Python, the letter ‘u’ works as a prefix to distinguish between Unicode and usual strings.
print (u' Hello Python!!') #Hello Python
7. Built-in String Functions in Python
Conversion Functions
1. capitalize() – Returns the string with the first character capitalized and the rest of the characters in lowercase.
var = 'PYTHON' print (var.capitalize()) # Python
2. lower() – Converts all the characters of the string to lowercase.
var = 'TechBeamers' print (var.lower()) # techbeamers
3. upper() – Converts all the characters of the string to uppercase.
var = 'TechBeamers' print (var.upper()) # TECHBEAMERS
4. swapcase() – It swaps the case of every character in a given string. All lowercase characters change to uppercase and vice-versa.
var = 'TechBeamers' print (var.swapcase()) # tECHbEAMERS
5. title() – Returns the ‘title cased’ version of the string, which means that all words start with uppercase and the rest of the characters in words are in lowercase.
var = 'welcome to Python programming' print (var.title()) # Welcome To Python Programming
6. count( str[, beg [, end]]) – Returns the number of times substring ‘str’ occurs in the range [beg, end] if beg and end index are given else the search continues in full string Search is case-sensitive.
var='TechBeamers' str='e' print (var.count(str)) # 3
var1='Eagle Eyes' print (var1.count('e')) # 2
var2='Eagle Eyes' print (var2.count('E',0,5)) # 1
Comparison Functions – Part1
1. islower() – Returns ‘True’ if all the characters in the string are in lowercase. If any of the char is in uppercase, it will return False.
var='Python' print (var.islower()) # False var='python' print (var.islower()) # True
2. isupper() – Returns ‘True’ if all the characters in the string are in uppercase. If any of the char is in lowercase, it will return False.
var='Python' print (var.isupper()) # False var='PYTHON' print (var.isupper()) # True
3. isdecimal() – Returns ‘True’ if all the characters in a string are decimal. If any character in the string is of another data type, it will return False.
Decimal characters are those from the Unicode category Nd.
num=u'2016' print (num.isdecimal()) # True
4. isdigit()
– Returns ‘True’ for any character which isdecimal()
would return ‘True and some characters in the ‘No’ category. If there are any characters other than these, it will return False’.
Precisely, digits are the characters for which Unicode property includes: Numeric_Type=Digit or Numeric_Type=Decimal.
For example, superscripts are digits, but fractions are not.
print ('2'.isdigit()) # True print ('²'.isdigit()) # True
Comparison Functions – Part2
1. isnumeric()
– Returns ‘True’ if all the characters of the Unicode string lie in any one of the categories Nd, No, and NI.
If there are any characters other than these, it will return False.
Precisely, Numeric characters are those for which the Unicode property includes: Numeric_Type=Digit, Numeric_Type=Decimal, or Numeric_Type=Numeric.
num=u'2016' print (num.isnumeric()) # True num=u'year2016' print (num.isnumeric()) # False
2. isalpha()
– Returns ‘True’ if the string contains at least one character (non-empty string), and all the characters are alphabetic, ‘False’ otherwise.
print ('python'.isalpha()) # True print ('python3'.isalpha()) # False
3. isalnum()
– Returns ‘True’ if the string contains at least one character (non-empty string), and all the characters are either alphabetic or decimal digits, ‘False’ otherwise.
print ('python'.isalnum()) # True print ('python3'.isalnum()) # True
Padding Functions
1. rjust(width[,fillchar]) – It populates the string with the fill char while pushing the original content to the right.
In the absence of the fill char, Python uses the space char.
var='Python' print (var.rjust(10)) # Python print (var.rjust(10,'-')) # ----Python
2. ljust(width[,fillchar])
– Returns a padded version of the string with the original string left-justified to a total of width columns
If the fill char is not present, then the space char fills the string.
var='Python' print (var.ljust(10)) # Python print (var.ljust(10,'-')) # Python----
3. center(width[,fillchar]) – Returns string filled with the input char while pushing the original content into the center.
Please note that space becomes the fill char if not specified.
var='Python' print (var.center(20)) # Python print (var.center(20,'*')) # *******Python*******
4. zfill(width)
– Returns string filled with the original content padded on the left with zeros so that the total length of the string becomes equal to the input size.
If there is a leading sign (+/-) present in the string, then with this function, padding starts after the symbol, not before it.
var='Python' print (var.zfill(10)) # 0000Python var='+Python' print (var.zfill(10)) # +000Python
Search Functions
1. find(str [,i [,j]])
– Searches for ‘str’ in the complete string (if i and j are not defined) or in a sub-string of string (if i and j are defined). This function returns the index if ‘str’ is found else returns ‘-1’.
Here, i=search starts from this index, and j=search ends at this index.
See more details – Python String Find()
var="Tech Beamers" str="Beam" print (var.find(str)) # 5 var="Tech Beamers" str="Beam" print (var.find(str,4)) # 5 var="Tech Beamers" str="Beam" print (var.find(str,7)) # -1
2. index(str[,i [,j]])
– This is the same as the ‘find’ method. The only difference is that it raises the ‘ValueError’ exception if ‘str’ doesn’t exist.
var='Tech Beamers' str='Beam' print (var.index(str)) # 5 var='Tech Beamers' str='Beam' print (var.index(str,4)) # 5 var='Tech Beamers' str='Beam' print (var.index(str,7)) # ValueError: substring not found
3. rfind(str[,i [,j]])
– This is the same as find() just that this function returns the last index where ‘str’ is found. If ‘str’ is not found, it returns ‘-1’.
var='This is a good example' str='is' print (var.rfind(str,0,10)) # 5 print (var.rfind(str,10)) # -1
4. count(str[,i [,j]])
– Returns the number of occurrences of substring ‘str’ in the string. Searches for ‘str’ in the complete string (if i and j are not defined) or in a sub-string (if i and j are defined).
Where: i=search starts from this index, and j=search ends at this index.
var='This is a good example' str='is' print (var.count(str)) # 2 print (var.count(str,4,10)) # 1
String Substitution Functions
1. replace(old,new[,count])
– Replaces all the occurrences of substring ‘old’ with ‘new’ in the given string.
If the count is available, then only that much number of occurrences of ‘old’ will be replaced with the ‘new’ var.
Where old =substring to replace, new =substring
var='This is a good example' str='was' print (var.replace('is',str)) # Thwas was a good exampleprint (var.replace('is',str,1)) # Thwas is a good example
2. split([sep[,maxsplit]]) – Returns a list of substrings obtained after splitting the string with ‘sep’ as a delimiter.
Where, sep= delimiter, the default is space, maxsplit= number of splits to be done
var = "This is a good example" print (var.split()) # ['This', 'is', 'a', 'good', 'example']print (var.split(' ', 3)) # ['This', 'is', 'a', 'good example']
3. splitlines(num)
– Splits the string at line breaks and returns the list after removing the line breaks.
Where num = if this is a positive value. It indicates that line breaks will appear in the returned list.
var='Print new line\nNextline\n\nMove again to new line' print (var.splitlines()) # ['Print new line', 'Nextline', '', 'Move again to new line']print (var.splitlines(1)) # ['Print new line\n', 'Nextline\n', '\n', 'Move again to new line']
4. join(seq) – Returns a string obtained after concatenating the sequence ‘seq’ with a delimiter string.
Where: the seq= sequence of elements to join
seq=('ab','bc','cd') str='=' print (str.join(seq)) # ab=bc=cd
Misc String Functions
1. lstrip([chars])
– Returns a string after removing the characters from the beginning of the string.
Where: Chars=this is the character to be trimmed from the string.
The default is a whitespace character.
var=' This is a good example ' print (var.lstrip()) # This is a good example var='*****This is a good example*****' print (var.lstrip('*')) # This is a good example**********
2. rstrip()
– Returns a string after removing the characters from the end of the string.
Where: Chars=this is the character to be trimmed from the string. The default is a whitespace character.
var=' This is a good example ' print (var.rstrip()) # This is a good example var='*****This is a good example*****' print (var.lstrip('*')) # *****This is a good example
3. rindex(str[,i [,j]])
– Searches for ‘str’ in the complete string (if i and j are not defined) or in a sub-string of string (if i and j are defined). This function returns the last index where ‘str’ is available.
If ‘str’ is not there, then it raises a ValueError exception.
Where: i=search starts from this index, and j=search ends at this index.
var='This is a good example' str='is' print (var.rindex(str,0,10)) # 5 print (var.rindex(str,10)) # ValueError: substring not found
4. len(string) – Returns the length of the given string
var='This is a good example' print (len(var)) # 22
Conclusion
In this post, we tried to cover most of the string functionality available in Python. And we hope that you will get a better understanding of the Python strings.
However, if you have any questions regarding Python strings, please let us know. We’ll try to solve it at the earliest possible time.
Also, in Python 3.6, a new style of strings was introduced, known as f-strings, you should learn it too to keep up with the trends.
All the Best,
TechBeamers