Display numbers and letters assigned to variables in python print

For Python beginners, it's not easy to search for variables and text at the same time in the print statement, so I'd like to explain it here.

Print the characters assigned to the variable x with print

Here, we will explain three methods.

  1. Write text + variables

  2. Use conversion specification characters such as% d and% s

  3. Use the format function

  4. Write text + variables

test1.py



x = 3
print "x is"+str(x)+"is"
 

test1.py output


'x is 3'

Don't forget to convert X to string type.

  1. Use conversion specification characters such as% d and% s

test2.py



x = 3
print "x is%d" % x
 

test2.py output


'x is 3'

Also, it seems that you can write as follows using locals ().

test2_1.py



x = 3
print "x is%(x)s" % locals()

test2_1.py output


'x is 3'
  1. Use the format function

test3.py



x = 3
print "x is{0}is".format(x)
 

test3.py output


'x is 3'

test3_1.py



x = 3
print "x is{x}is".format(**locals())
 

test3_1.py output


'x is 3'

Personally, I find .format () useful. You don't have to worry about the type of variables in format, and if the number of variables increases, you can just increase them like {0}, {1} .... The format at the end should be (variable 1, variable 2 ...).

Addition: There was an indication on September 24, 2015, so I corrected it

Recommended Posts

Display numbers and letters assigned to variables in python print
How to put a half-width space before letters and numbers in Python.
Application to display and search local memos (diary) in Python
I was addicted to confusing class variables and instance variables in Python
How to display bytes in the same way in Java and Python
How to access environment variables in Python
[Python] How to display random numbers (random module)
How to dynamically define variables in Python
How to display multiplication table in python
To reference environment variables in Python in Blender
How to display Hello world in python
How to use is and == in Python
How to generate permutations in Python and C ++
Python variables and data types learned in chemoinformatics
Receive and display HTML form data in Python
Send messages to Skype and Chatwork in Python
Practice applying functions and global variables in Python
I want to display the progress in Python!
To represent date, time, time, and seconds in Python
How to plot autocorrelation and partial autocorrelation in python
Use os.getenv to get environment variables in Python
Prime numbers in Python
Convert timezoned date and time to Unixtime in Python2.7
[Python] Convert decimal numbers to binary numbers, octal numbers, and hexadecimal numbers
[Python] How to expand variables in a character string
Difference between Ruby and Python in terms of variables
How to convert floating point numbers to binary numbers in Python
Write tests in Python to profile and check coverage
[Python] How to sort dict in list and instance in list
How to add page numbers to PDF files (in Python)
Python OpenCV tried to display the image in text.
I want to replace the variables in the python template file and mass-produce it in another file.
Script to count and stop up to 5 seconds in Python in Blender
Assigned scaffolding macro in Python script file to F12 key
Try to make it using GUI and PyQt in Python
To flush stdout in Python
How to swap elements in an array in Python, and how to reverse an array.
Display UTM-30LX data in Python
Login to website in Python
[python] Use DataFrame to label arbitrary variables and arrays together and save them in csv [pandas]
[Python] How to play with class variables with decorator and metaclass
Handle environment variables in Python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Reference order of class variables and instance variables in "self. Class variables" in Python
How to define multiple variables in a python for statement
[Introduction to Python3 Day 3] Chapter 2 Py components: Numbers, strings, variables (2.2-2.3.6)
Tips for coding short and easy to read in Python
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
A standard way to develop and distribute packages in Python
Useful tricks related to list and for statements in Python
Python class variables and instance variables
Comparison of how to use higher-order functions in Python 2 and 3
Mayungo's Python Learning Episode 3: I tried to print numbers with print
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
Stack and Queue in Python
Speech to speech in python [text to speech]
Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
[Introduction to Python3 Day 2] Chapter 2 Py Components: Numbers, Strings, Variables (2.1)
Python variables and object IDs
Object-oriented in C: Refactored "○ ✕ game" and ported it to Python
Unittest and CI in Python