[Python] Various combinations of strings and values

Introduction

Python: 3.7.4

If you combine the string and the value as they are in Python, an error will occur.

str_int.py


NAME="Taro"
AGE=15

print(NAME + " is " + AGE + " years old")

Execution result (failure example)

An error occurred because the string and value cannot be combined as they are.

output


Traceback (most recent call last):
  File "str_int.py", line 5, in <module>
    print(NAME + " is " + AGE + " years old")
TypeError: can only concatenate str (not "int") to str

solution

There are various solutions. Personally, I like 2 because it looks like programming. If you write only the points 1: Convert the value (AGE) to a string using str () 2:% s = character string,% d = value, so if you do not write this correctly, an error will occur 4: If you forget the leading "f", the variable name string will be output as it is without the variable being assigned. 5: If you connect with ",", a space will be automatically inserted and you will not need to use str (). <2019/12/22: Item 5 added due to konandoiruasa's point>

str_int2.py


NAME="Taro"
AGE=15

print("1 : " + NAME + " is " + str(AGE) + " years old")
print("2 : %s is %d years old" % (NAME, AGE))
print("3 : {} is {} years old".format(NAME, AGE))
print(f"4 : {NAME} is {AGE} years old")
print("5 :", NAME, "is", AGE, "years old")

Execution result (success example)

Everything went well. Taro is 15 years old.

output


1 : Taro is 15 years old
2 : Taro is 15 years old
3 : Taro is 15 years old
4 : Taro is 15 years old
5 : Taro is 15 years old

Recommended Posts

[Python] Various combinations of strings and values
Various processing of Python
Links and memos of Python character code strings
[Python] Summary of conversion between character strings and numerical values (ascii code)
3-3, Python strings and character codes
About various encodings of Python 3
Source installation and installation of Python
[Python] Types of statistical values (features) and calculation methods
[python] plot the values ​​before and after the conversion of yeojohnson conversion
Python: Create a dictionary from a list of keys and values
Environment construction of python and opencv
Various of Tweepy. Ma ♡ and ♡ me ♡
The story of Python and the story of NaN
Installation of SciPy and matplotlib (Python)
This and that of python properties
Coexistence of Python2 and 3 with CircleCI (1.0)
Summary of Python indexes and slices
Reputation of Python books and reference books
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
Installation of Visual studio code and installation of python
Change the length of Python csv strings
Extraction of tweet.js (json.loads and eval) (Python)
Summary of various for statements in Python
Connect a lot of Python or and and
Bulk replacement of strings in Python arrays
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
Various format specifications of str.format () method of Python3
Easy introduction of python3 series and OpenCV3
Idempotent automation of Python and PyPI setup
Full understanding of Python threading and multiprocessing
Project Euler # 1 "Multiples of 3 and 5" in Python
Introduction of Python
Correspondence summary of array operation of ruby and python
Python: Tips-Swap values
Summary of the differences between PHP and Python
[python] Create a list of various character types
Python numpy ignores very small values ​​and displays
The answer of "1/2" is different between python2 and 3
Calculate the total number of combinations with python
[Python] Takes representative values ​​of multiple images [Numpy]
Installation of Python3 and Flask [Environment construction summary]
Basics of Python ①
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
Basics of python ①
Compare the speed of Python append and map
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
# 3 [python3] Various operators
Copy of python
python development environment -use of pyenv and virtualenv-
[Django3] Environment construction and various settings summary [Python3]
Comparison of R and Python writing (Euclidean algorithm)
List of Python code to move and remember
[Python] Chapter 02-02 Basics of Python programs (Handling of character strings)
[Python] A rough understanding of iterators, iterators, and generators
Remove specific strings at the end of python
About the * (asterisk) argument of python (and itertools.starmap)
A discussion of the strengths and weaknesses of Python
About shallow and deep copies of Python / Ruby
[Data science memorandum] Handling of missing values ​​[python]
Continuation of multi-platform development with Electron and Python
Explanation of edit distance and implementation in Python