- 字符串
Python 语言中,字符串是用两个双引号""或者单引号''括起来的零个或多个字符
字符串中的字母和中文都算一个字符且空格也算
字符串也提供区间访问方式,可采用[N:M]格式,表示字符串中从N到M(不包含M)的子字符串
字符串的序列可正向递增和反向递减
如 : a="12345" 如果提取要取12可表示为a[0:2]或者a[-5:-3]
- 标识符
标识符就是程序中,使用的各种名称,例如:变量名、常量名、类名等等。
第一个字符必须是字母表中的字母或下划线 _ ;
标识符的其他的部分,由字母、数字和下划线组成;
标识符对大小写敏感;
标识符不能与保留字相同。
保留字
保留字即关键字,是 Python 语言中内部使用的单词,代表一定语义
可用 import keyword 输出所有的关键字
print(keyword.kwlist)
- 注释
python 中单行注释以 # 开头
多行注释可以使用多个#,也可以用(一对)'''(三个单引号)或者"""(三个双引号)包裹要注释的内容
4.输入输出
print函数可以同时输出多个内容,只需要将它一起放在print的括号内,并用逗号隔开:
如:print("hello","world") 得到的结果:hello world 不过同时输出的多个内容之间,会有空格隔开;
Python 的print也能实现格式化输出,方法是使用%操作符,它会将左边的字符串当做格式字符串,将右边的参数代入格式字符串:
如: print("100 + 200 = %d" % 300) #左边的%d被替换成右边的300
print("A的小写是%s" % "a") #左边的%s被替换成右边的a
如果要带入多个参数,则需要用()包裹代入的多个参数,参数与参数之间用逗号隔开,参数的顺序应该对应格式字符串中的顺序:
如: print("%d + %d = %d" % (100,200,300) #输出结果是100+200=300
print("%s %s" % ("world","hello")) #输出结果是world hellow
input()函数可采用格式化输出方式,通过format()方法将待输出变量整理成期望输出的格式
如: >>>name=input("请输入一个人的名字:") ###请输入一个人的名字:郭靖
>>>country=input("请输入一个国家的名字:") ###请输入一个国家的名字:中国
print("{}来自于{}".format(name,country)) ###输出:郭靖来自于中国
使用input函数可以获得用户输入,使用变量 = input()的形式将其赋值给一个变量,还可在input()的括号内加一些提示信息
如:使用input函数可以获得用户输入,在控制台窗口上,输入的一行的字符串,使用变量 = input()的形式将其赋值给一个变量:
str1 = input() ####运行之后,会先显示请输入:,输入数据hello之后回车,则会得到输出:输入的是hello,控制台上显示的全部内容为:
print("输入的是%s" % str1) #### 请输入:hello
输入的是hello
字符串转换:input函数接收的是用户输入的字符串,此时还不能作为整数或者小数进行数学运算,需要使用函数将字符串转换成想要的类型。
转换成整数,使用int()函数:num1 = int(str)
转换成小数,使用float()函数:f1 = float(str)
如: 1. str = input()
2. num1 = int(str)
3. f1 = float(str)
4. print("整数%d,小数%f" % (num1,f1)) #### 如果输入10,得到的输出是:整数10,小数10.000000
- 字符串拼接
常见的有以下几种:
5.1 使用加号(+)运算符:
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name) #### 输出:John Doe
5.2使用逗号(,)将多个字符串作为参数传递给print()函数:
first_name = "John"
last_name = "Doe"
print(first_name, last_name) #### 输出:John Doe
5.3使用字符串的format()方法:
first_name = "John"
last_name = "Doe"
full_name = "{} {}".format(first_name, last_name)
print(full_name) #### 输出:John Doe
5.4使用f-string(格式化字符串):
first_name = "John"
last_name = "Doe"
full_name = f"{first_name} {last_name}"
print(full_name) #### 输出:John Doe
-
字符的转换
6.1 大小写转换
Python 提供了upper()和lower()方法,来对字符串进行大小写转换。
其中,upper()会将字符串中的所有字符都转换为大写,lower()则将所有字符转换为小写。
Python 还提供了title()方法,将字符串所有单词的首字母变成大写,而其他字母依然小写。各个方法的具体语法如下:say_hello = 'Dear my Daughter'## 创建一个字符串say_hello
upper_say_hello = say_hello.upper() ## 使用upper()方法对say_hello字符串进行处理
lower_say_hello = say_hello.lower() ## 使用lower()方法对say_hello字符串进行处理
title_say_hello = say_hello.title() ## 使用title()方法对say_hello字符串进行处理print (say_hello+"\n") 输出:Dear my Daughter
print (upper_say_hello+"\n") DEAR MY DAUGHTER
print (lower_say_hello+"\n") dear my daughter
print (title_say_hello+"\n") Dear My Daughter6.2 去除字符串首尾空格
Python 提供了strip()方法,可以去除字符串两侧(不包含内部)全部的空格。使用该方法,也可以通过指定参数,去除两侧指定的特定字符。注意:在指定参数时,如果参数是多个字符,则该方法会将多个字符逐个去比对,进行删除(区分大小写),直到首尾两侧没有匹配的字符为止。但是,该方法对字符串中间的字符没有影响。
基本语法如下:
strip_string1 = source_string.strip()
string_strip2 = source_string.strip(target_char)hello_world = ' The world ** is big! ' # 创建一个字符串hello_world
blank_hello_world = hello_world.strip() # 利用strip()方法处理hello_world字符串
char_hello_world = hello_world.strip('TH *')
print(blank_hello_world) ### 输出:*The world ** is big!*
print(char_hello_world) ### 输出:he world ** is big! ### strip()方法将源字符串首尾所有空格、* 以及字符T去掉了,而源字符串中头部的h因为是小写并没有去除 ###6.3 字符串长度获取
Python 提供了len()函数来计算,并返回字符串的长度,即字符串中单个元素的个数。其基本语法如下:
length = len(target_string)创建一个字符串变量,获取其长度并打印出来
color = 'It is red'
length = len(color)
print (length) ####输出:9
直接在len函数中引入字符串内容获得其长度,然后打印出来
print(len('This is a circle!')) ####输出:17
注意:从输出结果可以看到,空格也占一个字符元素的位置。 -
字符串查找与替换
7.1 字符串查找
Python 提供了内置的字符串查找方法find(),利用该方法可以在一个较长的字符串中查找子字符串。
如果该字符串中,有一个或者多个子字符串,则该方法返回第一个子串所在位置的###最左端索引###,若没有找到
符合条件的子串,则返回-1source_string = 'The past is gone and static' ## 创建一个字符串
print(source_string.find('past')) ## 查看"past"在source_string字符串中的位置 ###输出:4
print(source_string.find('love')) ## 查看"love"在source_string字符串中的位置 ###输出:-17.2 字符串替换
Python 提供了replace()方法,用以替换给定字符串中的子串。如:在如下字符串中,用small子串替换big子串:
source_string = 'The world is big' ### 创建一个字符串circle
print(source_string.replace('big','small')) ### 利用replace()方法用子串"small"代替子串"big"
### 输出:The world is small7.3 字符串分割
Python 提供了split()方法实现字符串分割。该方法根据提供的分隔符,将一个字符串分割为字符列表,如果不提供
分隔符,则程序会默认把空格(制表、换行等)作为分隔符如: 用+、/还有空格作为分隔符,分割字符串:
source_string = '1+2+3+4+5'
利用split()方法,按照+
和/
对source_string字符串进行分割
print(source_string.split('+')) ###输出:['1', '2', '3', '4', '5']
print(source_string.split('/')) ###输出:['1+2+3+4+5'] -
列表元素的增删改
8.1 添加列表元素
Python 提供了append()和insert()等函数,实现向一个列表增加新元素的功能。
(1)在列表尾部添加元素:可以使用append()方法向一个列表的尾部追加一个元素,其基本语法如:source_list.append(obj)
如:要向guests列表尾部增加客人Hu qi,相应的语句为:
guests=['Zhang san','Li si','Wang wu','Zhao liu']# 初始化guests列表
guests.append('Hu qi')# 向guests列表尾部追加一个名为Hu qi的客人
print(guests) # 输出新的guests列表 ##输出:['Zhang san','Li si','Wang wu','Zhao liu','Hu qi']
(2)在列表指定位置添加元素:可以使用insert()方法,可以在列表任意指定位置插入元素,其基本语法为:source_list.insert(index,obj)
如,要向guests列表中Zhang san的后面增加客人Hu qi,则相应的语句为:
创建并初始化guests列表
guests=['Zhang san','Li si','Wang wu','Zhao liu']
向guests列表Zhang san后面增加一个名为Hu qi的客人
guests.insert(1,'Hu qi')
输出新的guests列表
print(guests)
输出结果为:['Zhang san','Hu qi','Li si','Wang wu','Zhao liu']
8.2 修改列表元素
Python 中修改列表元素的方法为:直接将列表中要修改的元素索引指出,然后为其指定新值。其基本语法如:source_list[index] = obj
例如,将请客名单guests列表中的Wang wu改为Wang shi,则相应的语句为:
初始化guests列表
guests=['Zhang san','Li si','Wang wu','Zhao liu']
将列表中的Wang wu
改为Wang shi
guests[2] = 'Wang shi'
输出新的guests列表
print(guests) 输出结果为:['Zhang san','Li si','Wang shi','Zhao liu']
8.3 删除列表元素
(1)删除指定位置的元素:
del方法
在 Python 中,调用del函数能够删除指定索引位置的元素,其基本语法如下:
del source_list[index]
例如,将请客名单guests列表中的Zhang san删除,则相应的语句为:
初始化guests列表
guests=['Zhang san','Li si','Wang wu','Zhao liu']
将列表中的Zhang san
删除
del guests[0]
输出新的guests列表
print(guests)
输出结果为:['Li si','Wang wu','Zhao liu']
pop方法
Python 还提供了pop()方法来删除元素,该方法将从源列表删除对应元素,同时返回被删除的元素。其基本语法如下:
deleted_obj = source_list.pop(index) 注意:index参数为可选项,不填则默认删除列表末尾的元素。
例如,将请客名单guests列表中的Zhang san删除,则相应的语句为:
初始化guests列表
guests=['Zhang san','Li si','Wang wu','Zhao liu']
将列表中的Zhang san
删除
deleted_obj = guests.pop(0)
输出被删除的元素以及删除后的guests列表
print(deleted_obj)
print(guests) 输出结果为:Zhang san ,['Li si','Wang wu','Zhao liu']
(2)删除指定值对应的元素:Python 提供了remove()方法,可以直接通过元素值来删除对应的元素。其基本语法如下:
source_list.remove(obj) 注意:如果列表中有多个值为obj的元素,remove仅删除位置索引最靠前的那个元素。
例如,将请客名单guests列表中的Zhang san删除,则相应的语句为:
初始化guests列表,列表中有多个名为Zhang san的客人
guests=['Zhang san','Li si','Wang wu','Zhao liu','Zhang san']
将列表中的`Zhang san`删除
guests.remove('Zhang san')
输出新的guests列表
print(guests) 输出结果为:['Li si','Wang wu','Zhao liu','Zhang san']