String
Python - String
Section titled “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'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)`str1=‘This is a string in Python’ print(str1)
str2=“This is a string in Python” print(str2)`Try it Multi-line strings must be embed in triple quotes, as shown below.
str1='''This isthe firstMulti-line string.'''print(str1)
str2="""This isthe secondMulti-linestring."""print(str2)`str1='''This is the first Multi-line string. ''' print(str1)
str2="""This is the second Multi-line string.""" print(str2)`Try it 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)`str1=‘Welcome to “Python Tutorial” on TutorialsTeacher’ print(str1)
str2=“Welcome to ‘Python Tutorial’ on TutorialsTeacher”
print(str2)[Try it](/codeeditor?cid=python-3z7p9wkj3) Use the len() function to retrieve the length of a string, as shown below. len()`
greet='Hello'n = len(greet) #returns 5greet='Hello' n = len(greet) #returns 5Try it
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'print(greet[0])print(greet[1])print(greet[2])print(greet[3])print(greet[4])print(greet[5]) #IndexError: string index out of rangegreet='hello' print(greet[0]) print(greet[1]) print(greet[2]) print(greet[3]) print(greet[4]) print(greet[5]) #IndexError: string index out of rangeTry it
Python supports negative indexing too, starting with -(length of string) till -1.
greet='hello'print(greet[-5])print(greet[-4])print(greet[-3])print(greet[-2])print(greet[-1])print(greet[0])greet='hello' print(greet[-5]) print(greet[-4]) print(greet[-3]) print(greet[-2]) print(greet[-1]) print(greet[0])Try it
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' #TypeError:'str' object does not support item assignmentgreet='hello' greet[0]='A' #TypeError:'str' object does not support item assignment
str Class
Section titled “str Class”All strings are objects of the str class in Python.
str
greet='hello'print(type(greet)) #output: <class 'str'>greet='hello' print(type(greet)) #output: <class 'str'>Try it
Use the str() function to convert a number to a string.
str()
s = str(100)print(s) #'100'
s = str(-10)print(s) #'-10'
s = str(True)print(s) #'True'`s = str(100) print(s) #‘100’
s = str(-10) print(s) #‘-10’
s = str(True) print(s) #‘True’`Try it
Escape Sequences
Section titled “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)`str1=‘Welcome to ‘Python Tutorial’ on TutorialsTeacher’ print(str1)
str2=“Welcome to “Python Tutorial” on TutorialsTeacher”
print(str2)[Try it](/codeeditor?cid=python-3z7pau6e6) Use r or R to ignore escape sequences in a string. r“R`
str1 = r'Welcome to 'Python Tutorial' on TutorialsTeacher'print(str1)str1 = r'Welcome to 'Python Tutorial' on TutorialsTeacher' print(str1)Try it
The following table lists escape sequences in Python.
String Operators
Section titled “String Operators”Obviously, arithmetic operators don’t operate on strings. However, there are special operators for string processing.
String Methods
Section titled “String Methods”str.capitalize()string.casefold()string.center()string.count()string.endswith()string.expandtabs()string.find()string.index()string.isalnum()string.isalpha()string.isascii()string.isdecimal()string.isdigit()string.isidentifier()string.islower()string.isnumeric()string.isprintable()string.isspace()string.istitle()string.isupper()string.join()string.ljust()string.lower()string.lstrip()string.maketrans()string.partition()string.replace()string.rfind()string.rindex()string.rjust()string.rpartition()string.rsplit()string.rstrip()string.split()string.splitlines()string.startswith()string.strip()string.swapcase()string.title()string.translate()string.upper()string.zfill()