This is a memorandum of learning Python. We hope that it will be helpful for beginners in programming and those who are also interested in other languages.
For me, who is new to learning Python, the word "** string literal " was the first time for me, so I wondered what " string literal **" was.
"** A string literal is a constant that indicates a continuous character string of 0 or more characters, which is described in programming languages. ** ”
It's a bit like the content I wrote in the previous article, but when defining a string in a Python program, it was to enclose a group of multiple characters in single quotes (') or double quotes ("). .. You've defined string literals for some time.
Be careful when using single quotes (') or double quotes (") as characters.
For example, if you define a string literal containing single quotation marks such as "Bob's toy" as follows, "'Bob'" enclosed in the first and second single quotation marks is judged to be a string literal. The result is an error because "s toy'" is a syntactically incorrect description.
script.py
print('Bob's toy')
If you want to use single quotes as characters in a string like this, enclose the string in double quotes.
script.py
print("Bob's toy")
Conversely, if you want to use double quotes as characters in a string, enclose the string in single quotes.
script.py
print('I give her"Good morning"said')
There is also a way to use escape sequences, but I would like to post it as a separate article.
Reference article Write a string literal
Recommended Posts