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.