In Python, a string is a sequence of characters enclosed inside quotation marks. For example,
text = 'Hi there'
print(text) # Hi there
Here, 'Hi there'
is a string. It contains 'H'
, 'i'
, ' '
, 't'
, 'h'
, 'e'
, 'r'
and 'e'
characters.
Creating Strings
A string is created by characters inside either single quotes or double quotes.
# single quote
text = 'Hello there'
print(text) # Hello there
# double quotes
text = "Hello there"
print(text) # Hello there
We can either use single quotes or double quotes for strings. However, we cannot mismatch quotations to create strings.
# mismatch quotation to create string
text = 'Hello there"
print(text)
Output
ERROR! File "<string>", line 2 text = 'Hello there" ^ SyntaxError: unterminated string literal (detected at line 2)
Multiline Strings
To create a string that extends multiple lines, we can use either triple single quotes or triple double quotes.
# triple single quotes for multiline strings
text = '''Hello there.
How are you doing?'''
print(text)
# triple double quotes for multiline strings
text = """Howdy.
What's up?"""
print(text)
Output
Hello there. How are you doing? Howdy. What's up?
Escape Sequence
If a string contains quotation marks inside strings like this:
text = "He said, "What's there?""
print(text)
We will get an error. It's because Python thinks "He said, "
is a string and the remaining What's there?""
is an invalid syntax.
To fix this, we can escape characters using a backslash \
before it.
text = "He said, \"What\'s there?\""
print(text)
Output
He said, "What's there?"
Escape sequences are also used to print characters that we cannot directly type such as newline and tabs.
print('Hello.\n\nHow are you?')
Output
Hello. How are you?
Here, the \n
is an escape sequence that prints a new line.
Similarly, if we want to print a tab, we can use the \t
escape sequence.
Access String Characters
The characters of a string are in order, and we can access individual characters of a string using indexes. For example,
text = 'bing'
# access the first item
print(text[0]) # b
# access the third item
print(text[2]) # n
In Python, indexing starts from 0. Meaning,
- the index of the first string character is 0
- the index of the second character is 1
- and so on
Hence, text[0]
gives the first character of the text
string, text[1]
gives the second character, and so on.
Negative Indexing
Python strings also support negative indexing.
- the index of the last character is -1
- the index of the second last character is -2
- and so on
text = 'bing'
# access the last character
print(text[-1]) # g
# access the third last character
print(text[-3]) # i
By the way, if the specified index does not exist in the string, Python throws the IndexError
exception.
text = 'bing'
# trying to access the fifth character
# however, the string only has four characters
print(text[4])
Output
Traceback (most recent call last): File "<string>", line 5, in <module> IndexError: string index out of range
Slicing of a String
It is also possible to access a substring from the string using the slicing notation. For example,
text= ('Datamentor')
# string with characters at index 0, 1, 2 and 3
print(text[0: 4]) # Data
# string with characters at index 4, 5 and 6
print(text[4: 7]) # men
One thing to remember about string slicing is that the start index is inclusive whereas the end index is exclusive.
Hence,
text[0: 4]
returns a substring with characters at index 0, 1, 2, and 3 but not 4.languages[4: 7]
returns a substring with characters at index 4, 5, and 6 but not 7.
Slicing: Skip Start and End Index
If we use the empty start index, the slicing starts from the beginning of the string. Similarly, if we use the empty end index, the slicing ends at the last index.
text = 'Datamentor'
# chracters from index 0 to 3
print(text[:4]) # Data
# chracters from index 4 to last
print(text[4:]) # mentor
Change String Characters
Python strings are immutable. Meaning we cannot change the characters of a string once created.
If we try to change the characters of a string, we will get an error.
text = 'Datamentor'
# trying to change the first character
text[0] = 'C'
print(text)
Output
Traceback (most recent call last): File "<string>", line 5, in <module> ERROR! TypeError: 'str' object does not support item assignment
However, we can reassign a different string to the same name if we need to change characters.
text = 'Datamentor'
# the + operator concatenates strings
# concatenating 'C' and 'atamentor'
text = 'C' + text[1:]
print(text)
Here, we can not modify the 'Datamentor'
string; it's not possible.
Instead, we are creating a new string by concatenating 'C'
and 'atamentor'
and we have assigned it to the same text variable.
Concatenating String
As mentioned above, we can concatenate (join) two strings by using the +
operator.
text1 = 'Data'
text2 = 'Mentor'
# concatenating strings: text1 and text2
result = text1 + text2
print(result) # DataMentor
# concatenating strings" text1, ' ' and text2
print(text1 + ' ' + text2) # Data Mentor
Delete Strings
We cannot remove characters of a string because strings are immutable. However, we can delete the string itself using the del
statement.
text = 'DataMentor'
# deleting the text string
del text
print(text) # Error: name 'text' is not defined
Python String Length
We can find the length of a string using the len()
function. For example,
text = 'DataMentor'
print(len(text)) # 10
Python Strings Methods
Python has many useful methods that make it really easy to work with strings. Here are the commonly used string methods
Method | Description |
---|---|
capitalize() |
converts the first character of the string to uppercase |
center() |
creates a new centered string with the given character |
count() |
returns the count of the given character/substring |
endswith() |
checks if the string ends with the given suffix |
find() |
returns the index of the first occurrence of the given substring |
format() |
formats strings into a nicer output |
index() |
returns the index of the first matching substring |
isalnum() |
checks if the string only contains alphanumeric (alphabet or number) characters or not |
isalpha() |
checks if the string only contains alphabets or not |
isdigit() |
checks if all characters of the string are numbers or not |
join() |
joins items of an iterable (containing strings) |
lower() |
converts the string into lower case |
replace() |
replaces the substring present in the string with a new string |
split() |
splits the string at the given separator and returns a list |
startswith() |
checks if the string starts with the given substring or not |
strip() |
removes the specified leading and trailing characters from the string |
upper() |
converts all lowercase characters in a string to uppercase characters and return it |
Python String Formatting and f-Strings
The string's format()
method makes it really easy to format strings. For example,
number1 = 5
number2 = 10
total = number1 + number2
output = '{0} + {1} = {2}'.format(number1, number2, total)
print(output) # 5 + 10 = 15
Here,
- the value of
number1
replaces{0}
- the value of
number2
replaces{1}
- the value of
number3
replaces{2}
Starting from Python 3.6, f-strings were introduced which is a great way to format strings.
The f-string starts with either 'f'
or 'F'
. For example,
number1 = 5
number2 = 10
total = number1 + number2
print(f'{number1} + {number2} = {total}') # 5 + 10 = 15
Iterating through a String
It's really easy to iterate through a string using the for loop.
text = 'data'
# iterating through the string
for letter in text:
print(letter)
Output
d a t a
Check if a Character/Substring Exists in the String
We can use the in
keyword to check if a substring exists in the string or not.
text = 'Datamentor'
print('Data' in text) # True
print('Data' not in text) # False
print('data' in text) # False
Python String Summary
In Python, a string is
- collection - a sequence of characters
- ordered - characters of a string are in order
- unchangeable - characters of a string cannot be changed
Recommended Reading: Python List