[Introduction to Udemy Python3 + Application] 11. Character strings

** * 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. ** **

■ Basic character string

print_standard


print('hello')
print("hello")

result


hello
hello

If you specify a string in print (), you can use either '' or " ".


■ When there is a "'" in the character string

◆ Enclose with " "

print_'


print("I don't know.")

result


I don't know.

If you enclose it in " ", it will be output properly.

◆ An error will occur if enclosed in ''

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.

◆ Use \ to avoid errors

print_'


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.


■ When there is "\ n" in the character string

◆ Line break \ 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.

◆ Incorrectly judged as \ 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.

◆ Avoid judgment using 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.


■ Line breaks

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".


■ Use operators

◆ Strings and operators

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 +.

◆ Parallel of variables and strings

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 +.

◆ If you prefer to omit +

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

[Introduction to Udemy Python3 + Application] 11. Character strings
[Introduction to Udemy Python3 + Application] 13. Character method
[Introduction to Udemy Python3 + Application] 14. Character substitution 15.f-strings
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
[Introduction to Udemy Python 3 + Application] 56. Closure
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Udemy Python 3 + Application] Summary
[Introduction to Udemy Python3 + Application] 18. List methods
[Introduction to Udemy Python3 + Application] 63. Generator comprehension
[Introduction to Udemy Python3 + Application] 28. Collective type
[Introduction to Udemy Python3 + Application] 33. if statement
[Introduction to Udemy Python3 + Application] 55. In-function functions
[Introduction to Udemy Python3 + Application] 48. Function definition
[Introduction to Udemy Python 3 + Application] 10. Numerical values
[Introduction to Udemy Python3 + Application] 21. Tuple type
[Introduction to Udemy Python3 + Application] 45. enumerate function
[Introduction to Udemy Python3 + Application] 41. Input function
[Introduction to Udemy Python3 + Application] 17. List operation
[Introduction to Udemy Python3 + Application] 65. Exception handling
[Introduction to Udemy Python3 + Application] 44. range function
[Introduction to Udemy Python3 + Application] 46. Zip function
[Introduction to Udemy Python3 + Application] 24. Dictionary type
[Introduction to Udemy Python3 + Application] 8. Variable declaration
[Introduction to Udemy Python3 + Application] 29. Set method
[Introduction to Udemy Python3 + Application] 16. List type
[Introduction to Udemy Python3 + Application] 61. Dictionary comprehension
[Introduction to Udemy Python 3 + Application] 22. Tuple unpacking
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
[Introduction to Udemy Python3 + Application] 23. How to use tuples
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
[Introduction to Udemy Python 3 + Application] 19. Copy of list
[Introduction to Udemy Python 3 + Application] 38. When judging None
[Introduction to Udemy Python3 + Application] 40.while else statement
[Introduction to Udemy Python3 + Application] 62. Set comprehension notation
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
[Introduction to Udemy Python3 + Application] 43. for else statement
[Introduction to Udemy Python3 + Application] 67. Command line arguments
[Introduction to Udemy Python3 + Application] 9. First, print with print
[Introduction to Udemy Python 3 + Application] 54. What is Docstrings?
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
[Introduction to Udemy Python3 + Application] 53. Dictionary of keyword arguments
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
[Introduction to Udemy Python3 + Application] 68. Import statement and AS
[Introduction to Udemy Python3 + Application] 52. Tupleization of positional arguments
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
[Introduction to Udemy Python3 + Application] 39. while statement, continue statement and break statement
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Introduction to Udemy Python3 + Application] 32.1 When one line becomes long
[Introduction to Udemy Python3 + Application] 50. Positional arguments, keyword arguments, and default arguments
[Introduction to Udemy Python3 + Application] 51. Be careful with default arguments
[Introduction to Udemy Python3 + Application] 49. Function citation and return value declaration
[Introduction to Udemy Python3 + Application] 69. Import of absolute path and relative path
Introduction to Python language
Introduction to OpenCV (python)-(2)
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
3-3, Python strings and character codes