Last time, there were many explanations about operations between numbers, but this time I would like to deal with characters and character strings.
First, try entering the following program from the ** Python Console **. Then the calculation result will be displayed as shown below.
>>>print(10+20)
30
Next, enclose the "10 + 20" part with "'" (single quotation marks) and execute.
>>>print('10+20')
10+20
In Python, if you enclose a number in ** single quotation marks, it will be treated as a string. ** ** Try entering the following program from the ** Python Console **. Then, the characters will be concatenated as shown below.
>>>print('10'+'20')
1020
Here, + (plus) is not an addition, but a concatenation of character strings. This is called the ** concatenation operator **.
You can also use \ * (asterisk) to repeat. Try entering the following program from the ** Python Console **. Then, the specified number of minutes will be displayed as shown below.
>>>print('Yes' * 3)
YesYesYes
Try entering the following program from the ** Python Console **. Assign the string '100' to the variable s. (It is a character string because it is enclosed in single quotation marks) Furthermore, when the contents of s are displayed, the character string '100' is displayed as shown below.
>>>s = '100'
>>>s
'100'
Now, in this state, subtract the number (not the string) from s. Then, the following error will occur.
>>>s - 10
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'int'
What does this error mean? "The operation of'str' (string) and'int' (integer) is not supported." is what it means.
In other words, it can be calculated by changing the variable s from a character string to a numerical value. So how do you convert it? This converts the character string assigned to the variable s to a numerical value as follows, and then subtracts it.
>>>int(s) - 10
90
** int function ** is a function for converting to an integer. This will change '100' to the number 100 and it will be calculated.
Try entering the following program from the ** Python Console **. This time I would like to go without using the print () function. (You can use it)
>>>'Number is ' + 5
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
This is the same as before, which means that you cannot add (or combine) strings and numbers.
This time, I would like to convert the number 5 to a character string. Try entering the following program from the ** Python Console **.
>>>'Number is ' + str(5)
'Number is 5'
** str function ** is a function that converts a numerical value into a character string. By doing this, you can confirm that the join is possible. This time I tried with the number 5, but it can be converted to a character string by using a variable with an appropriate number.
This time, I touched on the conversion of strings and numbers. It seems that the error that often occurs when programming and executing is often caused by forgetting to convert. This type conversion of character strings and numbers is often used, so please wear it. The functions that came out this time are as follows.
Recommended Posts