Basics of Python learning ~ What is a string literal? ~

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.


What is a string literal?

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.

What if I use single or double quotes in a string?

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

Basics of Python learning ~ What is a string literal? ~
Python learning basics ~ What is type conversion? ~
What is a python map?
[Python] What is a zip function?
[Python] What is a with statement?
Python learning basics ~ How to output (display) a character string? ~
A beginner's summary of Python machine learning is super concise.
[Learning memo] Basics of class by python
What kind of programming language is Python?
Basics of Python ①
Basics of python ①
What is a dog? Python installation volume
[python] [meta] Is the type of python a type?
What is python
A memorandum of python string deletion process
What is Python
What is the XX file at the root of a popular Python project?
[Example of Python improvement] What is the recommended learning site for Python beginners?
Get a glimpse of machine learning in Python
[Python] Chapter 02-05 Basics of Python programs (string operations / methods)
Tell me what a conformal map is, Python!
Python: Unsupervised Learning: Basics
[Python] What is Pipeline ...
What is a terminal?
Basics of Python scraping basics
What is a hacker?
What is a pointer?
# 4 [python] Basics of functions
What is ensemble learning?
Basics of python: Output
[Python] What is virtualenv
What is machine learning?
Check if the string is a number in python
What is a recommend engine? Summary of the types
What is God? Make a simple chatbot with python
[Python] Use a string sequence
Is this string a decimal?
python: Basics of using scikit-learn ①
Python list is not a list
Supervised learning 1 Basics of supervised learning (classification)
What is a Context Switch?
[Python] Python and security-① What is Python?
What is a super user?
[Python] * args ** What is kwrgs?
What is a system call
[Definition] What is a framework?
What is a callback function?
Basics of Python × GIS (Part 1)
Python Basic Course (1 What is Python)
Find out the apparent width of a string in python
Determine if a string is a time with a python regular expression
Python> Read from a multi-line string instead of a file> io.StringIO ()
What is the default TLS version of the python requests module?
What beginners learned from the basics of variables in python
A record of patching a python package
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
Create an instance of a predefined class from a string in Python
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
A good description of Python decorators
Is it a character string operation?