[Python] Chapter 02-02 Basics of Python programs (Handling of character strings)

[Python] Chapter 02-02 Handling of strings

Last time, there were many explanations about operations between numbers, but this time I would like to deal with characters and character strings.

Strings and concatenation operators

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

String to number conversion

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.

Conversion from number to string

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.

Finally

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.

Return to [Table of Contents Link]

Recommended Posts

[Python] Chapter 02-02 Basics of Python programs (Handling of character strings)
[Python] Chapter 02-06 <Supplement> Basics of Python programs (handling of numerical values)
[Python] Chapter 02-03 Basics of Python programs (input / output)
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
[Python] Chapter 02-05 Basics of Python programs (string operations / methods)
Basics of Python ①
[Python] Chapter 02-04 Basics of Python Program (About Comments)
Basics of Python scraping basics
[Python of Hikari-] Chapter 05-06 Control Syntax (Basics of Comprehension)
Links and memos of Python character code strings
# 4 [python] Basics of functions
Basics of network programs?
[Python for Hikari] Chapter 09-01 Classes (Basics of Objects)
Basics of python: Output
[Basics of Modern Mathematical Statistics with python] Chapter 1: Probability
3-3, Python strings and character codes
Static analysis of Python programs
python> Handling of 2D arrays
Handling of python on mac
python: Basics of using scikit-learn ①
Basics of Python × GIS (Part 1)
[Python] How to make a list of character strings character by character
Paiza Python Primer 5: Basics of Dictionaries
Handling of sparse tree-structured attributes (Python)
[Python of Hikari-] Chapter 09-03 Class (inheritance)
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python3 Day 14] Chapter 7 Strings (7.1.1.1 to 7.1.1.4)
Getting Started with Python Basics of Python
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
Review of the basics of Python (FizzBuzz)
Basics of Python x GIS (Part 2)
About the basics list of Python basics
[Excel] Addition of character strings (combination)
Learn the basics of Python ① Beginners
[Python of Hikari-] Chapter 07-02 Exception handling (continuous execution of the program by exception handling)
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
[Basics of Modern Mathematical Statistics with python] Chapter 3: Typical Probability Distribution
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
Python basics ⑤
Change the length of Python csv strings
Python basics
[Learning memo] Basics of class by python
[Python3] Understand the basics of Beautiful Soup
Python basics ④
I didn't know the basics of Python
[Python] Summary of conversion between character strings and numerical values (ascii code)
Python basics
[Introduction to Udemy Python3 + Application] 11. Character strings
The basics of running NoxPlayer in Python
[Basics of python basics] Why do __name__ == "__main__"
Bulk replacement of strings in Python arrays
[Python] Chapter 05-02 Control Syntax (Combination of Conditions)
Python learning memo for machine learning by Chainer Chapter 13 Basics of neural networks
Python basics
Python basics
Python basics ③
[Python] Various combinations of strings and values
Python basics ②
Python basics ②
[Introduction to Data Scientists] Basics of Python ♬