In Python, anything enclosed between single or double quotation marks is a string. A string is a sequence or array of textual data. For example:
name = "King Pin"
You can enclose strings in either single or double quotes, and the output will remain the same.
Multiline strings can be created using three double quotes at the beginning and end of the string, like this:
str = """
If our string has multiple lines, we can create them like this
by using three double quotes at the beginning and end instead of single quotes.
"""
In Python, a string is like an array of characters, and you can access parts of a string using its index, which starts from 0. Square brackets are used to access elements of the string. For example:
print(name[0])
print(name[1])
The above code will print the first and second characters of the string name
.
K
i
You can loop through strings using a for loop, like this:
for character in name:
print(character)
Each character of the string name
is printed on a separate line.
K
i
n
g
P
i
n