Out of all the Python Data Types strings are probably the most common.
Strings are text enclosed in quotes, either single or double quotation marks.
“This is a string in Python”
‘This is also a string in Python’
If you have a multi-line string you need to surround it in triple quotes.
“”” This is a string
that will span
many lines “””
You can also use three single quotes.
Python String Methods
Python has a set of built-in methods that you can use against strings.
For a full list of string methods check here – https://docs.python.org/3/library/stdtypes.html
An example of a Python string method is split()
Python String Split
You can split a string into a list, the default separator is whitespace.
sentence = "This is a sentence with no separators"
x = txt.split()
print(x)
The output of this will be a list separated by the whitespace.
['This', 'is', 'a', 'sentence', 'with', 'no', 'separators']
If you want to split the string at a certain point, you define the separator as below.
sentence = "This is a sentence, with a comma"
x = txt.split(", ")
print(x)
The output from this will be a list generated from the string but split at the comma.
['This is a sentence', 'with a comma']
Python String Format
Python f-string
Python String Concatenation
Next Lesson – Python Dictionaries