How to convert Python # type for Python super beginners: str

environment windows7 (I want a Mac Book Pro 16inch) Visual Studio Code chrome python ver3.8.3

This article is written for beginners in programming and Python.

1. How to convert Python type

str integer was a number (integer) and string was a character (string). If you try to output a number (integer) by concatenating it with a character (character string) as it is, it will be as follows.

style.py


calc = 100 * 3.14
answer = "100 times the circumference" + calc + "is"
print(answer)

#answer = "100 times the circumference" + calc + "is"
#TypeError: can only concatenate str (not "float") to str

I got an error. TypeError:can only concatenate str (not "float") to str This error is the error "Cannot concatenate because the type is different". Since the sentence "100 times the pi is" and the sentence "is" is a character (string) and the variable calc is a number (although it is a floating point number) "Let's arrange characters (character strings) and numbers (integers and floating point numbers) before concatenating them." It is a promise.

In such a case, enclose the character (character string) as it is in "" and convert the number (integer, floating point number) to the character (character string). Then, should I enclose it in Calc with" "?

type.py


calc = 100 * 3.14
answer = "100 times the circumference" + "calc" + "is"
print(answer)

#100 times the circumference is calc

I have recognized calc as a character (character string) as it is. Let's change it as follows. This is where the theme str comes into play.

style.py


calc = 100 * 3.14
answer = "100 times the circumference" + str(calc) + "is"
print(answer)
#100 times the circumference is 314.0

Yes, it was output successfully. What's different about the above two codes is answer = "100 times the pi is" + calc +" " answer = "100 times the pi is" + str (calc) + "" And just added str () to calc.

Use str () to arrange (convert) numbers (integers, floating point numbers) into characters (strings). The above example uses variables, but the simpler one is easier to understand.

type.py


print(type(123))
#<class 'int'>

print(type(str(123)))
#<class 'str'>

The upper part of the above example is 123 Is a number (integer) I'm saying. The bottom row is str(123) Is a character (character string) I'm saying.

This is how to convert type: str.

Recommended Posts

How to convert Python # type for Python super beginners: str
How to convert Python # type for Python super beginners: int, float
Python # How to check type and type for super beginners
Python for super beginners Python # dictionary type 1 for super beginners
Python for super beginners Python # dictionary type 2 for super beginners
Python for super beginners Python for super beginners # Easy to get angry
How to make Python faster for beginners [numpy]
Python #function 2 for super beginners
Python for super beginners Python #functions 1
Python #list for super beginners
~ Tips for beginners to Python ③ ~
[For beginners] How to use say command in python!
[For beginners] How to study Python3 data analysis exam
Python #index for super beginners, slices
Python #len function for super beginners
Python #Hello World for super beginners
How to learn TensorFlow for liberal arts and Python beginners
[Python] How to convert db file to csv
[Python] How to convert a 2D list to a 1D list
How to convert Python to an exe file
Let's put together Python for super beginners
[Python] Organizing how to use for statements
[For beginners] How to register a library created in Python in PyPI
How to use "deque" for Python data
Memo # 4 for Python beginners to read "Detailed Python Grammar"
[Python] How to use two types of type ()
How to make Spigot plugin (for Java beginners)
Memo # 3 for Python beginners to read "Detailed Python Grammar"
How to install Python for pharmaceutical company researchers
Memo # 1 for Python beginners to read "Detailed Python Grammar"
How to use data analysis tools for beginners
How to convert SVG to PDF and PNG [Python]
Try to calculate RPN in Python (for beginners)
Easy understanding of Python for & arrays (for super beginners)
Memo # 2 for Python beginners to read "Detailed Python Grammar"
How to get dictionary type elements of Python 2.7
Memo # 7 for Python beginners to read "Detailed Python Grammar"
How to convert (32,32,3) to 4D tensor (1,32,32,1) with ndarray type
Convert exponential notation float to str in Python
Introduction to Programming (Python) TA Tendency for beginners
Memo # 6 for Python beginners to read "Detailed Python Grammar"
How to handle datetime type in python sqlite3
[For beginners] How to study programming Private memo
Memo # 5 for Python beginners to read "Detailed Python Grammar"
About Python external module import <For super beginners>
How to install Python
How to install python
python textbook for beginners
OpenCV for Python beginners
[BigQuery] How to use BigQuery API for Python -Table creation-
How to convert / restore a string with [] in python
How to run python in virtual space (for MacOS)
How to write a list / dictionary type of Python3
How to convert floating point numbers to binary numbers in Python
Python --Notes when converting from str type to int type
Python beginners talk about how to remember this much
How to convert JSON file to CSV file with Python Pandas
python, php, ruby How to convert decimal numbers to n-ary numbers
Convert Webpay Entity type to Dict type (recursively in Python)
[Introduction to Python] How to write repetitive statements using for statements
Tips for Python beginners to use the Scikit-image example for themselves 7 How to make a module