Hello Pythonistas, here’s a quick reference to the types of strings in Python. And when should you use which quotation?
Contents
Types Of String
There are three3️⃣ types of strings in python:
- single line,
- multiline, and
- empty.
Single-Line
Consider this like a toothpaste box 📁that can contain only one tube.
Single line strings end in one single line, or even a single word or a character.
Example:
print("I love python") #here, I love python is a single line string.
print('l') #l
print('python') #python
Output:-
I love python
l
python
Multiline
The second is multiline strings consider a carton📦 that can contain a lot of toothpaste tubes.
These occupy more than one line.
Here’s another example of it:
print('''this is a
multiline
string''')
Output:
this is a
multiline
string
One thing to note✍ here is you can also use double triple quotes for multiline strings(""" """
like this).
Do you remember I said(ok wrote) there is something called unassigned strings in this post?
That meant it is not compulsory to assign a string to a variable or use it inside of the print function. You can use it outside freely in the program.
"This is an unassigned string."
However, it won’t display any output and also wouldn’t be available to use until assigned to a variable or used inside the print function.
Empty
Consider these as small showpiece box, that cannot contain anything.
Empty strings do not have any content inside of them that’s why they are called empty.
For example:-
print("")
#NOTE: They don't even have white spaces.
Where to use single, double, and triple quotes
Now, you might be confused🤔 ok got this but I still doubt where to use single, double, and triple quotes.
The answer is pretty simple, you can use any of the quotes for a single line and an empty string in python.
It is compulsory to use triple quotes for multiline strings.
Letter = 's'
One_sentence_str = "Use double quotes for me"
Multi_str = '''you do not have
any other options for me'''
It is better if you use single quotes for empty and one-character strings. And use double quotes for a word or a se