Outputting a string to the screen is one of the basics of a program. Use the "print function" to output to the screen in Python. The print function is indispensable for using Python. This time, I will explain how to use the print function. table of contents 1 [What is print](What is ## print) 2 [Print variables with print](## Print variables with print) 2.1 [Print only variables with print](### Print only variables with print) 2.2 [format function](### format function) 2.3 [% notation](###% notation) 2.4 [String + Variable](### String + Variable)
The print function is a function that outputs a character string to the screen. The basic syntax of the print function is as follows.
Python
print('The character string you want to output')
To output a character with print, put the character string you want to output in the argument part of print. Since print adds a line break at the end of the character, the output character string is automatically broken. If you don't want a line break, use the end option to add nothing to the end.
print('Hello', end='')
print('World')
Execution result
HelloWorld
Also, in the case of Python2, you can eliminate line breaks by adding "," after the character string.
print('Hello' ,)
print('World')
Execution result
HelloWorld
In Python, you can store strings and numbers in variables. You can also use print to print strings and numbers stored in variables.
strings = 'Hello, World!'
number = 100
print(strings)
print(number)
Execution result
Hello, World!
100
You can now print variables using print. However, when printing a variable with print, you may want to write it together with an arbitrary character string. For example, when a certain calculation result reaches 100, it is easier to understand if you output something like "Answer: 100" rather than just displaying "100".
There are several ways to print strings and variables together in Python print.
A new method that can be used from Python 2.6 and above is to use the format function to output strings and variables. The format of the format function is as follows.
print ('arbitrary string {0} arbitrary string'.format (variable)
When using the format function, enter an arbitrary character string and enter {0} where you want to assign the variable. Then, the value of the variable that is the argument of format is displayed at the position of {0}.
number = 100
print('answer:{0}'.format(number))
Execution result
Answer: 100
If there are multiple variables you want to display, increase {0}, {1}, {2}…. And {}, and use format (variable 1, variable 2, variable 3… ..) as an argument in the format function. ) And so on.
answer1 = 50
answer2 = 100
print('Answer 1:{0},Answer 2:{1}'.format(answer1, answer2))
Execution result
Answer 1:50,Answer 2:100
The% notation is a method of replacing with a variable using the% operator. It's a familiar method in C language. Outputs character strings and variables using conversion specification characters such as% d and% s. The format of% notation is as follows.
print ('arbitrary string% s arbitrary string')% variable # When the content of the variable is a string print ('arbitrary string% d arbitrary string')% variable # When the content of the variable is an integer
There are several types of conversion specification characters, but the two most commonly used are "% s" when the content of the variable is a character string and "% d" when the content of the variable is an integer number.
answer = 50
print('answer:%d' % answer)
Execution result
answer:50
If you want to output multiple variables in% notation, use the tuple type.
name = 'Tanaka'
point = 80
print('%s score:%d' % (name, point))
Execution result
Tanaka score:80
The third method is to add the character string and the variable with the + operator and output it as one character string. In Python, you can use the + operator to add a string to a string or a variable that stores a string, and this is called concatenation. By concatenating the character strings in the print function, it can be output as it is as a single character string.
a = 'hello'
b = 'world!'
print(a + ',' + b)
Execution result
hello,world!
Only character strings can be concatenated, so when outputting numerical values etc., convert them to character strings before concatenating.
name = 'Tanaka'
point = 80
print(name + 'Score:' + str(point)) #Convert point to string with str function
Execution result
Tanaka score:80
・ About the handling of Japanese As you can see from the samples so far, Python print can of course output Japanese. However, if you try to output Japanese without thinking about anything, an error may occur.
Reference site [Introduction to Python] How to output a character string in a Print statement
Recommended Posts