Python - String

In Python, string is an immutable sequence data type. It is the sequence of Unicode characters wrapped inside single, double, or triple quotes.

The followings are valid string literals in Python.

'This is a string in Python' # string in single quotes
"This is a string in Python" # string in double quotes
'''This is a string in Python''' # string in triple quotes
"""This is a string in Python""" # string in triple double-quotes

A string literal can be assigned to a variable, as shown below.

str1='This is a string in Python' 
print(str1)
str2="This is a string in Python" 
print(str2)
Output:
This is a string in Python
This is a string in Python

Multi-line strings must be embed in triple quotes, as shown below.

str1='''This is 
the first
Multi-line string.
'''
print(str1)

str2="""This is
the second
Multi-line
string."""
print(str2)
Output:
This is 
the first
Multi-line string.

This is
the second
Multi-line
string.

If a string literal required to embed double quotes as part of a string then, it should be put in single quotes. Likewise, if a string includes a single quote as a part of a string then, it should be written in double quotes.

str1='Welcome to "Python Tutorial" on TutorialsTeacher'
print(str1)

str2="Welcome to 'Python Tutorial' on TutorialsTeacher"
print(str2)
    
Output:
Welcome to "Python Tutorial" from TutorialsTeacher
Welcome to 'Python Tutorial' on TutorialsTeacher

Use the len() function to retrieve the length of a string, as shown below.

>>> greet='Hello'
>>> len(greet)
5

A sequence is defined as an ordered collection of items. Hence, a string is an ordered collection of characters. The sequence uses an index, starting with zero to fetch a certain item (a character in case of a string) from it.

>>> greet='hello'
>>> greet[0]
'h'
>>> greet[1]
'e'
>>> greet[2]
'l'
>>> greet[3]
'l'
>>> greet[4]
'o'
>>> greet[5] # throw error if index > len(string)-1
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
greet[5]
IndexError: string index out of range

Python supports negative indexing too, starting with -(length of string) till -1.

>>> greet='hello'
>>> greet[-5]
'h'
>>> greet[-4]
'e'
>>> greet[-3]
'l'
>>> greet[-2]
'l'
>>> greet[-1]
'o'

The string is an immutable object. Hence, it is not possible to modify it. The attempt to assign different characters at a certain index results in errors.

>>> greet='hello'
>>> greet[0]='A'
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
greet[0]='A'
TypeError: 'str' object does not support item assignment

str Class

All strings are objects of the str class in Python.

>>> greet='hello'
>>> type(greet)
<class 'str'>

Use the str() function to convert a number to a string.

>>> str(100)
'100'
>>> str(-10)
'-10'
>>> str(True)
'True'

Escape Sequences

The escape character is used to invoke an alternative implementation of the subsequent character in a sequence. In Python, backslash \ is used as an escape character. Use a backslash character followed by the character you want to insert in a string e.g. \' to include a quote, or \" to include a double quotes in a string, as shown below.

str1='Welcome to \'Python Tutorial\' on TutorialsTeacher'
print(str1)

str2="Welcome to \"Python Tutorial\" on TutorialsTeacher"
print(str2)
    
Output:
Welcome to 'Python Tutorial' from TutorialsTeacher
Welcome to "Python Tutorial" on TutorialsTeacher

Use r or R to ignore escape sequences in a string.

str1=r'Welcome to \'Python Tutorial\' on TutorialsTeacher'
print(str1)
Output:
Welcome to \'Python Tutorial\' from TutorialsTeacher

The following table lists escape sequences in Python.

Escape sequence Description Example
\\ Backslash >>> "Hello\\Hi"
Hello\Hi
\b Backspace >>> "ab\bc"
ac
\f Form feed
\n Newline >>> "hello\nworld"
Hello
world
\nnn Octal notation, where n is in the range 0-7 >>> '\101'
A
\t Tab >>> 'Hello\tPython'
Hello    Python
\xnn Hexadecimal notation, where n is in the range 0-9, a-f, or A-F >>> '\x48\x69'
Hi
\onn Octal notation, where n is in the range 0-9 >>> "\110\151"
Hi
ADVERTISEMENT

String Operators

Obviously, arithmetic operators don't operate on strings. However, there are special operators for string processing.

Operator Description Example
+ Appends the second string to the first >>> a='hello'
>>> b='world'
>>> a+b
'helloworld'
* Concatenates multiple copies of the same string >>> a='hello'
>>> a*3
'hellohellohello'
[] Returns the character at the given index >>> a = 'Python'
>>> a[2]
t
[ : ] Fetches the characters in the range specified by two index operands separated by the : symbol >>> a = 'Python'
>>> a[0:2]
'Py'
in Returns true if a character exists in the given string >>> a = 'Python'
>>> 'x' in a
False
>>> 'y' in a
True
>>> 'p' in a
False
not in Returns true if a character does not exist in the given string >>> a = 'Python'
>>> 'x' not in a
True
>>> 'y' not in a
False

String Methods

Method Description
str.capitalize() Returns the copy of the string with its first character capitalized and the rest of the letters are in lowercased.
string.casefold() Returns a lowered case string. It is similar to the lower() method, but the casefold() method converts more characters into lower case.
string.center() Returns a new centered string of the specified length, which is padded with the specified character. The deafult character is space.
string.count() Searches (case-sensitive) the specified substring in the given string and returns an integer indicating occurrences of the substring.
string.endswith() Returns True if a string ends with the specified suffix (case-sensitive), otherwise returns False.
string.expandtabs() Returns a string with all tab characters \t replaced with one or more space, depending on the number of characters before \t and the specified tab size.
string.find() Returns the index of the first occurence of a substring in the given string (case-sensitive). If the substring is not found it returns -1.
string.index() Returns the index of the first occurence of a substring in the given string.
string.isalnum() Returns True if all characters in the string are alphanumeric (either alphabets or numbers). If not, it returns False.
string.isalpha() Returns True if all characters in a string are alphabetic (both lowercase and uppercase) and returns False if at least one character is not an alphabet.
string.isascii() Returns True if the string is empty or all characters in the string are ASCII.
string.isdecimal() Returns True if all characters in a string are decimal characters. If not, it returns False.
string.isdigit() Returns True if all characters in a string are digits or Unicode char of a digit. If not, it returns False.
string.isidentifier() Checks whether a string is valid identifier string or not. It returns True if the string is a valid identifier otherwise returns False.
string.islower() Checks whether all the characters of a given string are lowercased or not. It returns True if all characters are lowercased and False even if one character is uppercase.
string.isnumeric() Checks whether all the characters of the string are numeric characters or not. It will return True if all characters are numeric and will return False even if one character is non-numeric.
string.isprintable() Returns True if all the characters of the given string are Printable. It returns False even if one character is Non-Printable.
string.isspace() Returns True if all the characters of the given string are whitespaces. It returns False even if one character is not whitespace.
string.istitle() Checks whether each word's first character is upper case and the rest are in lower case or not. It returns True if a string is titlecased; otherwise, it returns False. The symbols and numbers are ignored.
string.isupper() Returns True if all characters are uppercase and False even if one character is not in uppercase.
string.join() Returns a string, which is the concatenation of the string (on which it is called) with the string elements of the specified iterable as an argument.
string.ljust() Returns the left justified string with the specified width. If the specified width is more than the string length, then the string's remaining part is filled with the specified fillchar.
string.lower() Returns the copy of the original string wherein all the characters are converted to lowercase.
string.lstrip() Returns a copy of the string by removing leading characters specified as an argument.
string.maketrans() Returns a mapping table that maps each character in the given string to the character in the second string at the same position. This mapping table is used with the translate() method, which will replace characters as per the mapping table.
string.partition() Splits the string at the first occurrence of the specified string separator sep argument and returns a tuple containing three elements, the part before the separator, the separator itself, and the part after the separator.
string.replace() Returns a copy of the string where all occurrences of a substring are replaced with another substring.
string.rfind() Returns the highest index of the specified substring (the last occurrence of the substring) in the given string.
string.rindex() Returns the index of the last occurence of a substring in the given string.
string.rjust() Returns the right justified string with the specified width. If the specified width is more than the string length, then the string's remaining part is filled with the specified fill char.
string.rpartition() Splits the string at the last occurrence of the specified string separator sep argument and returns a tuple containing three elements, the part before the separator, the separator itself, and the part after the separator.
string.rsplit() Splits a string from the specified separator and returns a list object with string elements.
string.rstrip() Returns a copy of the string by removing the trailing characters specified as argument.
string.split() Splits the string from the specified separator and returns a list object with string elements.
string.splitlines() Splits the string at line boundaries and returns a list of lines in the string.
string.startswith() Returns True if a string starts with the specified prefix. If not, it returns False.
string.strip() Returns a copy of the string by removing both the leading and the trailing characters.
string.swapcase() Returns a copy of the string with uppercase characters converted to lowercase and vice versa. Symbols and letters are ignored.
string.title() Returns a string where each word starts with an uppercase character, and the remaining characters are lowercase.
string.translate() Returns a string where each character is mapped to its corresponding character in the translation table.
string.upper() Returns a string in the upper case. Symbols and numbers remain unaffected.
string.zfill() Returns a copy of the string with '0' characters padded to the left. It adds zeros (0) at the beginning of the string until the length of a string equals the specified width parameter.