Home Backend Development Python Tutorial A closer look at Python variables and strings

A closer look at Python variables and strings

Mar 24, 2017 pm 04:46 PM

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":

A closer look at Python variables and strings

## 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.

A closer look at Python variables and strings

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:

A closer look at Python variables and strings

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'''

A closer look at Python variables and strings

##Next try to enter this paragraph in PyCharm Code:

A closer look at Python variables and strings

## Display after running:

A closer look at Python variables and strings

For my_intro, I added the first two variables, and then printed out my_intro, or you can directly print (what_do_i_do + what_i_like).

Here we talk about addition, of course, multiplication can also be done.

A closer look at Python variables and strings##After multiplying, you get:

A closer look at Python variables and stringsIf you want to

comment

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:

A closer look at Python variables and stringsGet 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 .

A closer look at Python variables and strings

Finally, try to solve a slightly more complicated problem:

A closer look at Python variables and strings

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

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 by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

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

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

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 without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

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...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

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...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

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)...

See all articles