[Introduction to Python] How to output a character string in a Print statement

[Introduction to Python] How to output a character string in a Print statement

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)

What is print

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

Print variables with print

Print only variables with print

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.

format function

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

%notation

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

String + variable

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

[Introduction to Python] How to output a character string in a Print statement
[Python] How to expand variables in a character string
[Python] How to invert a character string
Python learning basics ~ How to output (display) a character string? ~
[Introduction to Python] How to split a character string with the split function
[Introduction to Python] How to use the in operator in a for statement?
How to embed a variable in a python string
[Introduction to Python] How to write a character string with the format function
How to convert / restore a string with [] in python
How to define multiple variables in a python for statement
How to input a character string in Python and output it as it is or in the opposite direction.
[Introduction to Python] How to use class in Python?
How to get a stacktrace in python
How to make a string into an array or an array into a string in Python
How to get a string from a command line argument in python
How to quickly count the frequency of appearance of characters from a character string in Python?
How to clear tuples in a list (Python)
How to create a JSON file in Python
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
How to write a string when there are multiple lines in python
[Introduction to Python] Thorough explanation of the character string type used in Python!
Parse a JSON string written to a file in Python
I want to embed a variable in a Python string
How to write string concatenation in multiple lines in Python
Output a character string with line breaks in PyYAML
Introduction to Linear Algebra in Python: A = LU Decomposition
How to develop in Python
How to execute a command using subprocess in Python
[Python] How to output the list values in order
[Introduction to Python] How to get the index of data with a for statement
[Python] How to make a list of character strings character by character
How to slice a block multiple array from a multiple array in Python
How to output a document in pdf format with Sphinx
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
Change the standard output destination to a file in Python
How to import a file anywhere you like in Python
[Python] How to write an if statement in one sentence.
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
[Python] How to output a pandas table to an excel file
How to write a Python class
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
[Introduction to Python] How to parse JSON
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
6 ways to string objects in Python
How to use PubChem in Python
Create a random string in Python
How to handle Japanese in Python
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
Convert to a string while outputting standard output with Python subprocess
How to determine the existence of a selenium element in Python