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.
is defined as follows Variable a stores a value of numeric type.
a = 100
The variable b defined below stores a string type value.
b = "hello itcast.cn" 或者 b = 'hello itcast.cn'
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('--------------------------------------------------')
结果:
-------------------------------------------------- 姓名:ming 职位:讲师 公司地址:中山市昌平区建材城西路金燕龙办公楼1层 --------------------------------------------------
input通过它能够完成从键盘获取数据,然后保存到指定的变量中;
注意:input获取的数据,都以字符串的方式进行保存,即使输入的是数字,那么也是以字符串方式保存。
例:
userName = input('请输入用户名:') print("用户名为:%s"%userName) password = input('请输入密码:') print("密码为:%s"%password)
结果:(根据输入的不同结果也不同)
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.
Supermarket locker
#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:
如果想取出部分字符,那么可以通过下标
的方法,(注意Python中下标从 0 开始)
name = 'abcdef' print(name[0]) print(name[1]) print(name[2])
运行结果:
切片是指对操作的对象截取其中一部分的操作。字符串、列表、元组都支持切片操作。
注意:选取的区间属于左闭右开型,即从"起始"位开始,到"结束"位的前一位结束(不包含结束位本身)。
我们以字符串为例讲解。
如果取出一部分,则可以在中括号[]中,使用 :
例:
name = 'abcdef' print(name[0:3]) # 取 下标0~2 的字符
运行结果 :
例:
name = 'abcdef' print(name[0:5]) # 取 下标为0~4 的字符
运行结果:
例:
name = 'abcdef' print(name[3:5]) # 取 下标为3、4 的字符
运行结果:
例:
name = 'abcdef' print(name[2:]) # 取 下标为2开始到最后的字符
运行结果:
例:
name = 'abcdef' print(name[1:-1]) # 取 下标为1开始 到 最后第2个 之间的字符
运行结果:
>>> 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' #运行结果
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!