An article to help you understand the basic string knowledge of Python

Release: 2023-07-25 15:32:10
forward
1175 people have browsed it

Why do we need strings?

When calling a browser to log in to some websites, you need to enter a password. After the browser sends the password to the server, the server will verify the password. The verification process is to compare the previous The saved password is compared with the password passed this time. If they are equal, the password is considered correct, otherwise it is considered incorrect; since the server wants to store these passwords, it can use a database (such as MySQL) to achieve this.

Of course, for the sake of simplicity, we can first find a variable to store the password; so how to store passwords with letters? This is where strings are used.


1. The format of strings in Python

is defined as follows Variable a stores a value of numeric type.

   a = 100
Copy after login

The variable b defined below stores a string type value.

   b = "hello itcast.cn"
    或者
    b = 'hello itcast.cn'
Copy after login

Small summary:

  • Double quotes or single The data in quotation marks is the string

二、字符串输出

例:

name = 'ming'
position = '讲师'
address = '中山市平区建材城西路金燕龙办公楼1层'


print('--------------------------------------------------')
print("姓名:%s"%name)
print("职位:%s"%position)
print("公司地址:%s"%address)
print('--------------------------------------------------')
Copy after login

结果:

--------------------------------------------------
姓名:ming
职位:讲师
公司地址:中山市昌平区建材城西路金燕龙办公楼1层
--------------------------------------------------
Copy after login

三、字符串输入

input通过它能够完成从键盘获取数据,然后保存到指定的变量中;

注意:input获取的数据,都以字符串的方式进行保存,即使输入的是数字,那么也是以字符串方式保存。

例:

userName = input('请输入用户名:')
print("用户名为:%s"%userName)


password = input('请输入密码:')
print("密码为:%s"%password)
Copy after login

结果:(根据输入的不同结果也不同)

An article to help you understand the basic string knowledge of Python


4. Subscripts and slicing

1. Subscript index

So-called"Subscript" is the number, just like the number of the storage cabinet in the supermarket. You can find the corresponding storage space through this number.

"Subscript" in life

Supermarket locker

An article to help you understand the basic string knowledge of Python

The use of "subscript" in strings

#Lists and tuples support subscript indexing for easy understanding. Strings are actually arrays of characters. So subscript indexing is also supported.

If there is a string: name = 'abcdef', the actual storage in memory is as follows:

An article to help you understand the basic string knowledge of Python

如果想取出部分字符,那么可以通过下标的方法,(注意Python中下标从 0 开始)

name = 'abcdef'


 print(name[0])
 print(name[1])
 print(name[2])
Copy after login

运行结果:

An article to help you understand the basic string knowledge of Python

2. 切片的概念:

切片是指对操作的对象截取其中一部分的操作。字符串、列表、元组都支持切片操作。

3. 切片的语法:[起始:结束:步长]

注意:选取的区间属于左闭右开型,即从"起始"位开始,到"结束"位的前一位结束(不包含结束位本身)。

我们以字符串为例讲解。

如果取出一部分,则可以在中括号[]中,使用 :

例:

name = 'abcdef'


 print(name[0:3]) # 取 下标0~2 的字符
Copy after login

运行结果 :

An article to help you understand the basic string knowledge of Python

例:

name = 'abcdef'


 print(name[0:5]) # 取 下标为0~4 的字符
Copy after login

运行结果:

An article to help you understand the basic string knowledge of Python

例:

name = 'abcdef'


print(name[3:5]) # 取 下标为3、4 的字符
Copy after login

运行结果:

An article to help you understand the basic string knowledge of Python

例:

name = 'abcdef'


print(name[2:]) # 取 下标为2开始到最后的字符
Copy after login

运行结果:

An article to help you understand the basic string knowledge of Python

例:

name = 'abcdef'


print(name[1:-1]) # 取 下标为1开始 到 最后第2个  之间的字符
Copy after login

运行结果:

An article to help you understand the basic string knowledge of Python

 >>> a = "abcdef"
 >>> a[:3] #运行结果
 'abc'  
 >>> a[::2]  #运行结果
 'ace'
 >>> a[5:1:2]   
 ''  #运行结果
 >>> a[1:5:2]
 'bd' #运行结果
 >>> a[::-2]
 'fdb'  #运行结果
 >>> a[5:1:-2]
 'fd'   #运行结果
Copy after login

 

5. Summary

This article explains the basics of Python (strings) in detail. Introduced operations on strings and slicing. Subscript index. and provide solutions to problems encountered in actual operations. I hope it can help you learn Python better.

The above is the detailed content of An article to help you understand the basic string knowledge of Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Go语言进阶学习
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!