A collection of several characters is a string. Strings in Python must be surrounded by double quotes “” or single quotes ‘’. For example:
“Welcome to Free Learning Points”
‘Welcome to Free Learning Points’
The contents of the string can contain all the words in the world such as letters, punctuation, special symbols, other languages, etc.
The following are all valid strings:
“12345”
“1234ac”
“Hello World”
There is no difference between double quotes and single quotes in Python strings.
When quotes appear in the string content such as I’m, haven’t, etc, we need to do special treatment, otherwise Python will parse error.
‘I’m a great engineer.’
As the above string contains single quotes (I’m), thus Python will match the single quotes in the string with the first single quote and it will be treated only “I” as string, therefore subsequent “m a great engineer” becomes redundant content which resulting in syntax error.
For this situation, there are two options:
No. | Options | Details |
---|---|---|
1 | Using escape quotes | Add in a backslash (\) in front of the single quotes in order to escape the quotes and let Python treat it as normal text. |
2 | Using different quotes around the string | If single quotes appear in the string content (I’m), then you can use double quotes to surround the string and vice versa. |
Example of using escape quotes
str1 = "I'm a great coder!" #Enclose a string containing single quotes with double quotes str2 = 'double quote is " ' # use single quotes to surround strings containing double quotes print (str1) print (str2)
The output is:
I ' m a great coder!
double quote is "
Example of using different quotes around the string:
str1 = 'I \' m a great coder! ' str2 = "double quote is \"" print (str1) print (str2)
The output is:
I ' m a great coder!
double quote is "
Python is not a free form language. It is strict in syntax requirements for line indentation and line wrapping. You must add a backslash in a long sentence if you wish to have a newline. For example:
points = "It is nice to have you.\n I will prepare more exercises for you." print(points)
The output is:
It is nice to have you.
I will prepare more exercises for you.
The platform variable comes with a long string content, so the escape character (\) is used to split the whole long string into multiples lines.
Python’s long string is a string that can be written in a newline (without the backslash \). Python long strings are surrounded by three double quotes (“””) or three single quotes (‘’’), the syntax is shown as follows:
"""You can comment anything here!""" '''You can comment anything here!'''
The output is:
'You can comment anything here'
'You can comment anything here'
Placing single or double quotes in long strings will not cause parsing errors. If the long string is not assigned to any variable, then the long string will not have any effect, which is like a comment string.
When there is a long text content in the program that need to be defined as string type in Python, therefore the long string format is preferred because this long string format is very powerful. You can also place anything inside the string content including single and double quotes because it will not cause parsing error.
Example of assigning long string to a variable:
points = """It’s nice to have everyone support me. I will continue to prepare more exercises for you. """ print(points)
The output is:
It’s nice to have everyone support me.
I will continue to prepare more exercises for you.
Newlines, spaces, indentations and other whitespace characters in long string will be printed out the same as they are, so you cannot write them in this following format:
points = """It’s nice to have everyone support me. I will continue to prepare more exercises for you. """ print(points)
The output is:
It’s nice to have everyone support me.
I will continue to prepare more exercises for you.
Although the format is well presentable, but there are extra blank lines before and after the string content and extra spaces are added in front of each line.
The backslash (\) in Python strings has a special function, which is to escape characters. Escaping characters sometimes will cause some troubles, for example, I want to represent a string which containing the Windows path E:\Program Files\Python 3.8\python.exe.
It is not possible to write this Windows path in a Python program. Because of the special \, you need to escape each \ in the string which is written in the form E:\\Program Files\\Python 3.8\\python.exe.
With the above writing method, it requires special care and a slight overlook can make a mistake. To solve the problem of escape characters, Python also supports raw strings. In the raw strings, \ will not be treated as an escape character and everything will remain as “authentic”.
Adding the r prefix in front of a normal string or a long string can turn it into a raw string. The format is:
raw_1 = r"""You can comment anything here!""" raw_2 = r 'You can comment anything here!' print(raw_1) print(raw_2)
The output is:
You can comment anything here!
You can comment anything here!
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises