String format with Python% operator

Introduction

In addition to "%" as a remainder operator, Python has a method of using it as a format operator used for strings. (There is a similar usage in C language)

Addendum (2017/05/22): In python3, there are different methods such as format method, and the method using% operator is not recommended (see the comment at the bottom).

Basic writing

>>> #Uninflected word(python2)
>>> print 'Hello, %s' % 'world!' 
Hello, world!

>>> #Uninflected word(python3)
>>> print('Hello, %s' % 'world!')
Hello, world!

>>> #If there are multiple(python2)
>>> print 'My name is %s %s.' % ('python', 'qiita')
My name is python qiita.

>>> #General form without flag(python2)
>>> print '%(Conversion type)' % (Conversion source)

>>> #General form with flag(python2)
>>> print '%(flag)(Conversion type)' % (Conversion source)

Conversion type

Conversion type meaning
'd' Signed decimal integer
'i' Signed decimal integer
'x' Signed hexadecimal number(Lowercase)
'X' Signed hexadecimal number(uppercase letter)
'e' Floating point number in exponential notation(Lowercase)
'E' Floating point number in exponential notation(uppercase letter)
'f' Decimal floating point number
'F' Decimal floating point number
'c' One character
'r' String(repr()Convert with)
's' String(str()Convert with)

flag

flag meaning
'0' Zero padding for numeric types
'-' Left justify the converted value
' ' For signed conversions with positive numbers, leave a space before
'+' Prepend a code character to the conversion
Numbers(Non-zero) Output to that number of digits

References

-5.6.2. String formatting operations

Recommended Posts

String format with Python% operator
Python string format
Python string format
Python indentation and string format
Format json with Vim (with python)
String format
String format 2
Python string
String replacement with Python regular expression
[Python] Format when to_csv with pandas
Python: String concatenation
FizzBuzz with Python3
Scraping with Python
python string slice
Scraping with Python
Python with Go
[Python] Round up with just the operator
Twilio with Python
Integrate with Python
[Python] Use string data with scikit-learn SVM
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Python 3 operator memo
Bingo with python
Zundokokiyoshi with python
Python2 string type
Python # string type
format in python
Python string inversion
Excel with Python
Microcomputer with Python
Cast with python
[Introduction to Python] How to write a character string with the format function
Automatically format Python code into PEP8-compliant code with Emacs
Note for formatting numbers with python format function
Create a Photoshop format file (.psd) with python
Decrypt a string encrypted on iOS with Python
String to Unicode Escape Sequence Format for Python
Convert strings to character-by-character list format with python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python