Mayungo's Python Learning Episode 6: I tried to convert a character string to a number

This time, as the sixth episode, we will convert from character strings to numbers.

Click here for a list of each story.

The same content is also published in the video, so please have a look if you like.

Even if the same number is treated as a letter or a number, the processing method will change.

If it is a character with 114 and 514, it will stick to 114514 as it is. If it is a number, it will be processed as addition.

one = 114
two = 514

iti = "114"
ni = "514"

print(one + two)
print(iti + ni)

628
114514

However, you may want to enter it as a letter once and then treat it as a number.

At that time, put what you want to convert in int ().

Conversely, if you want to change from numbers to letters, put them in str ().

Here, one and two are letters, and iti and ni are numbers.

moji_one = str(one)
moji_two = str(two)
kazu_iti = int(iti)
kazu_ni = int(ni)

print(moji_one + moji_two)
print(kazu_iti + kazu_ni)

114514
628

It is the reverse of the very first time.

Once converted, even if you create an expression with the one that has not been converted, it will be processed without problems.

print(one + kazu_ni)
print(moji_one + ni)

628
114514

No error has occurred with one + kazu_ni and moji_one + ni.

Please note that if you make an expression with numbers and letters without conversion, an error will occur.

print(one + ni)

TypeError                                 Traceback (most recent call last)
<ipython-input-7-63f31269f628> in <module>
----> 1 print(one + ni)

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Summary String is str, number is int Can be converted with int (), str ()

Thank you for subscribing to the channel.

Recommended Posts

Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
Mayungo's Python Learning Episode 3: I tried to print numbers with print
Mayungo's Python Learning Episode 8: I tried input
I tried to generate a random character string
Mayungo's Python Learning Episode 2: I tried to put out characters with variables
Mayungo's Python Learning Episode 1: I tried printing with print
Mayungo's Python Learning Episode 5: I tried to do four arithmetic operations with numbers
Python learning basics ~ How to output (display) a character string? ~
I tried to automatically generate a character string to be input to Mr. Adjustment with Python
[Python] How to invert a character string
I tried to convert a Python file to EXE (Recursion error supported)
Mayungo's Python Learning Episode 7: I tried printing with if, elif, else
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I tried to discriminate a 6-digit number with a number discrimination application made with python
Mayungo's Python Learning Episode 4: I tried to see what happens when numbers are treated as letters
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
[Python] I tried to get the type name as a string from the type function
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
I tried to draw a route map with Python
[Python] How to expand variables in a character string
I tried to implement a pseudo pachislot in Python
I want to split a character string with hiragana
I tried to automatically generate a password with Python3
I tried to summarize the string operations of Python
I tried to solve AOJ's number theory with Python
[python] Convert date to string
I tried to make a real-time sound source separation mock with Python machine learning
I tried to implement a one-dimensional cellular automaton in Python
[Markov chain] I tried to read a quote into Python.
I tried "How to get a method decorated in Python"
I tried machine learning to convert sentences into XX style
I want to convert an ISO-8601 character string to Japan time
I tried to make a stopwatch using tkinter in python
I tried to divide with a deep learning language model
I tried to touch Python (installation)
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
Convert a string to an image
[5th] I tried to make a certain authenticator-like tool with python
Convert to a string while outputting standard output with Python subprocess
[2nd] I tried to make a certain authenticator-like tool with python
[Introduction to Python] How to split a character string with the split function
I tried to make a regular expression of "amount" using Python
[Introduction to Python] How to output a character string in a Print statement
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
Try to extract a character string from an image with Python3
I tried to create a list of prime numbers with python
I tried to make a regular expression of "date" using Python
I tried to implement a misunderstood prisoner's dilemma game in Python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to create a linebot (implementation)
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to create a linebot (preparation)