** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
print_standard
print('hello')
print("hello")
result
hello
hello
If you specify a string in print ()
, you can use either ''
or " "
.
" "
print_'
print("I don't know.")
result
I don't know.
If you enclose it in " "
, it will be output properly.
''
print_'
print('I don't know.')
result
print('I don't know.')
^
SyntaxError: invalid syntax
If you enclose it with ''
, the "'" in the character string will be judged as the end of the enclosing, resulting in an error.
\
to avoid errorsprint_'
print('I don\'t know.')
result
I don't know.
By putting \
before '
in the character string, you can avoid the judgment as a group.
print_'
print('say "I don\'t know."')
print("say \"I don't know.\"")
result
say "I don't know."
say "I don't know."
application.
\ n
print_\n
print('Hello. \nHow are you?')
result
Hello.
How are you?
I've dealt with the fact that \ n
becomes a" newline "before.
\ n
print_\n
print('C:\name\name')
result
C:
ame
ame
I wanted to display "C: \ name \ name", but the \ n
part was judged as a line break.
r
print_\n
print(r'C:\name\name')
result
C:\name\name
In that case, you can print the character string as it is by adding r
, which is the acronym of" raw ", to the beginning of the character string.
new_line
print("""
line1
line2
line3
""")
result
line1
line2
line3
By using " "" "" "
as above, you can write easy-to-read code without using
\ n` to write in one line.
new_line
print('#######')
print("""
line1
line2
line3
""")
print('#######')
result
#######
line1
line2
line3
#######
However, with this writing method, blank lines will be inserted above and below the character string.
new_line
print('#######')
print("""\
line1
line2
line3\
""")
print('#######')
result
#######
line1
line2
line3
#######
By putting \
in this way, it means "start the next code on the next line".
operator
print('Hi.' * 3 + 'Mike.')
print('Py' + 'thon')
print('Py''thon')
result
Hi.Hi.Hi.Mike.
Python
Python
You can also combine operators with strings.
If you simply connect the strings, they will be connected and printed without writing +
.
operator
prefix = ('Py')
print(prefix'thon')
result
print(prefix'thon')
^
SyntaxError: invalid syntax
However, when a character string is assigned to a variable, +
cannot be omitted.
operator
prefix = ('Py')
print(prefix + 'thon')
result
Python
I was able to connect properly with +
.
+
operator
print('aaaaaaaaaaaaaaaaaaaaaaaaaaa'
'bbbbbbbbbbbbbbbbbbbbbbbbbbb')
result
aaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbb
If you want to connect long strings and display them, writing them like this makes the code easier to read. (If you write in one line, it may not fit in the screen size and it is difficult to see.)
Recommended Posts