String literals
A string literal is a sequence of characters surrounded by quotes. We can use both single, double, or triple quotes for a string. And, a character literal is a single character surrounded by single or double quotes.
Example 7: How to use string literals in Python?
strings = "This is Python"
char = "C"
multiline_str = """This is a multiline string with more than one line code."""
unicode = u"\u00dcnic\u00f6de"
raw_str = r"raw \n string"
print(strings)
print(char)
print(multiline_str)
print(unicode)
print(raw_str)
Output
This is Python C This is a multiline string with more than one line code. Ünicöde raw \n string
In the above program, This is Python is a string literal and C is a character literal.
The value in triple-quotes
"""
assigned to the multiline_str is a multi-line string literal.
The string u"\u00dcnic\u00f6de" is a Unicode literal which supports characters other than English. In this case, \u00dc represents
Ü
and \u00f6 represents ö
.
r"raw \n string" is a raw string literal.
No comments:
Post a Comment