A closer look at Python variables and strings
LearnPythonThe same is true. The most convenient way to start learning a new language is to imitate, and then innovate through imitation. In the initial imitation process, it is necessary to type out every line of code and type out every punctuation mark in person, rather than simply reading ten lines at a glance without actual operation. In this way, even if you read the entire book, you may still not be able to write a program.
This is the second article about Python, mainly introducing variables and strings.
(1)
Variables:
Simply put, variables are the most basic storage units in programming, and they can store what you want to put in them. For example, assign a value to variable "a":
## Operation steps: Open the Mac terminal - enter "python3" and press Enter , enter the Python3.6 environment - enter "a=25" and press Enter. At this time, the assignment to a has been completed; enter a again, and after pressing Enter, you can see the assignment result to a.
Note: Python is case-sensitive, a and A are two different variables, please pay attention when writing.
print():
print() is a common function in Python, As the name suggests, it prints the content in the brackets. It can be understood simply this way. For example, assign a value of 25 to variable a, and then print a out. Enter the following content in PyCharm:
If you forget to assign a value to variable a, PyCharm will report an error during runtime. You need to report the error accordingly. Modify the prompt information accordingly.
The name "a" here is undefined, and Python cannot print objects that do not exist.
(2)
String:
Simply put, a string can express the content in single quotes, double quotes, or triple quotes. Here, single quotes and double quotes are the same.
#'Content in single quotes'
"Content in double quotes"
'''Triple quotes are usually used for longer content , you can wrap the line at will'''
##Next try to enter this paragraph in PyCharm Code:
Here we talk about addition, of course, multiplication can also be done.
##After multiplying, you get:
If you want to
out part of the code, directly select the part, and then use the shortcut key "command+/" to implement batch comments. Finally look at this code:
Get this result :
At this time, an error message appeared, indicating that it must be of str type, not int type. The reason is that string (string) is just a data type in Python, and the other data type is the integer type(integer ), two different data types cannot be added, and corresponding conversion is required. If you don’t know what type the variable is, you can enter print(type(Variable name)) in the compilation box to check the variable type. So, for the example of the error reported above, the correct way is to convert the string type to int, add the two, and finally get the result 1834 . Finally, try to solve a slightly more complicated problem: In Python, the len() method returns the length of the string. The length of string1 here is 22. Subtracting num, you will eventually get 2 Hellos! . At this point, you have basically mastered the basic usage of variables and strings. I will introduce the sharding and indexing of strings later. It is strongly recommended that you practice it yourself and type out these codes line by line. You may find some problems that cannot be detected visually. You can also draw inferences from one example during practice to experience the results after successful operation. A small sense of joy and accomplishment.
The above is the detailed content of A closer look at Python variables and strings. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
