字符串类型
-
python中的变量定义,可以用单引号或者双引号定义,不可一单一双,变量名不能以数字开头,名字不能与内置函数名冲突
-
>>> name = 'dingh' >>> name 'dingh' >>> age = 18 >>> my_float = 1.1 >>> age 18 >>> my_float 1.1 # 还可以同时赋值多个变量 >>> a = b = c = 1 >>> a,b,c (1, 1, 1) >>> >>> a,b,c = 1,2,3 >>> a,b,c (1, 2, 3)
-
-
接下来演示一些关于字符串的常用操作
-
字符串的定义与切片
# 定义一个变量 >>> my_str = 'hello world' # 切片操作: # 相当于走路一样 # 一共可以传3个参数 第一位代表起始位 第二位代表终止位 还有一位为步长,默认为1 # 切片是顾头不顾尾的,0:5 实际输出的是0到4的字符 # 切分 0:5 的字符,索引值默认从0开始,这里的0是可以省略的,默认从0开始 >>> my_str[0:5] 'hello' # 还是切分 0:5 的字符,这里有步长位,就是走两步输出一步 >>> my_str[0:5:2] 'hlo' # 这个是切片的一种倒着走的写法 >>> my_str[::-1] 'dlrow olleh'
-
这里先认识一下python的两个函数 dir() 与 help()来帮助新手
-
dir()函数
-
dir函数用来返回一个对象的方法列表
-
调用方法很简单 dir(object)
-
>>> dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
-
-
以上都字符串的所有方法,接下来介绍一些常用的方法
# capitalize (字符串的第一个单词的首字母大写) >>> my_str = 'hello world' >>> my_str.capitalize() 'Hello world' # title (将字符串每个单词首字符变成大写) >>> my_str = 'hello world' >>> my_str.title() 'Hello World' -------------------------------------------------------------------------------- # upper (将字符串字符都变成大写) >>> my_str = 'hello World' >>> my_str.upper() 'HELLO WORLD' # lower (将字符串都转换为小写) >>> my_str = 'Hello WORLD' >>> my_str.lower() 'hello world' -------------------------------------------------------------------------------- # isdecimal (判断字符串由否由十进制数字组成) >>> my_str = '123' >>> my_str.isdecimal() True >>> my_str = '1.1' >>> my_str.isdecimal() False # isdigit (判断字符串是否由整数组合) >>> my_str = '100' >>> my_str.isdigit() True >>> my_str = '100.1' >>> my_str.isdigit() False # isnumeric (判断字符串是否由中文数字罗马数字和阿拉伯数字组成) >>> my_str = '四' >>> my_str.isnumeric() True >>> my_str = '1' >>> my_str.isnumeric() True -------------------------------------------------------------------------------- # isalnum (判断字符串是否由数字或字母组成) >>> my_str = 'hello world' >>> my_str.isalnum() False >>> my_str = 'helloworld' >>> my_str.isalnum() True >>> my_str = 'hello123' >>> my_str.isalnum() True >>> my_str = 'hello!#$' >>> my_str.isalnum() False # isalpha (判断字符串是否由由字符组成) >>> my_str = 'hello' >>> my_str.isalpha() True >>> my_str = 'hell123' >>> my_str.isalpha() False -------------------------------------------------------------------------------- # islower (判断字符串是否都为小写) >>> my_str = 'Hello World' >>> my_str.islower() False >>> my_str = 'hello world' >>> my_str.islower() True # isupper (判断字符串是否都为大写) >>> my_str = 'HELLO WOLRD' >>> my_str.isupper() True >>> my_str = 'HELLO world' >>> my_str.isupper() False -------------------------------------------------------------------------------- # endswith (判断是否为什么结尾的) >>> my_str = 'hello world' >>> my_str.endswith('h') False >>> my_str.endswith('d') True # startswith (判断字符串是否以什么开头) >>> my_str = 'hello world' >>> my_str.startswith('h') True >>> my_str.startswith('hello') True >>> my_str.startswith('hello1') False -------------------------------------------------------------------------------- # isspace (判断字符串是否由空白字符组成) >>> my_str = '\n\r\t ' >>> my_str.isspace() True # istitle (判断字符串每个单词首字母是否为大写其他为小写) >>> my_str = 'Hello world' >>> my_str.istitle() False >>> my_str = 'Hello World' >>> my_str.istitle() True >>> my_str = 'Hello WOrld' >>> my_str.istitle() False -------------------------------------------------------------------------------- # find (查找字符串的索引位,没有则返回-1) >>> my_str = 'hello world' >>> my_str.find('e') 1 >>> my_str.find('a') -1 # index (查找字符串的索引位,没有则报错) >>> my_str = 'hello world' >>> my_str.index('h') 0 >>> my_str.index('o') 4 -------------------------------------------------------------------------------- # strip (去除字符串左右空白字符) >>> my_str = ' hello world ' >>> my_str.strip() 'hello world' # lstrip (去除字符串左边空白字符) >>> my_str = ' hello world ' >>> my_str.lstrip() hello world # rstrip (去除字符串右边空白字符) >>> my_str = ' hello wolrd ' >>> my_str.rstrip() ' hello wolrd' -------------------------------------------------------------------------------- # count (统计出现的次数) >>> my_str = 'hello world' >>> my_str.count('l') 3 -------------------------------------------------------------------------------- # format (字符串格式化) # 按照位置传参 >>> '{} {}'.format('hello','world') 'hello world' # 按照关键字传参 >>> '{str1} {str2}'.format(str1='hello',str2='world') 'hello world' # 按照索引传参 >>> '{1} {0}'.format('world','hello') 'hello world' -------------------------------------------------------------------------------- # join (字符串拼接) >>> my_str = 'hello' >>> '-'.join(my_str) 'h-e-l-l-o' -------------------------------------------------------------------------------- # replace (字符串替换) >>> my_str = 'hello world' >>> my_str.replace('world','china') 'hello china' # 指定替换次数 >>> my_str = 'hello world world world' >>> my_str.replace('world','china',2) 'hello china china world' -------------------------------------------------------------------------------- # split (字符串分割) >>> my_str = 'hello world' >>> my_str.split() ['hello', 'world'] # 指定分割次数 >>> my_str = 'hello world hello world' >>> my_str.split(' ',2) ['hello', 'world', 'hello world'] -------------------------------------------------------------------------------- # swapcase (将字符串大小写翻转) >>> my_str = 'hello WORLD' >>> my_str.swapcase() 'HELLO world' -------------------------------------------------------------------------------- # zfill (字符串填充,字符串右对齐,由0填充) >>> my_str = 'hello world' >>> my_str.zfill(30) '0000000000000000000hello world' # center (字符串填充,传递两个参数,第一个为输出多少位,第二个为占位符,字符串居中显示) >>> my_str = 'hello world' >>> my_str.center(30,'-') '---------hello world----------' # rjust (字符串填充,传递两个参数,第一个为输出多少位,第二个为占位符,字符串右对齐) >>> my_str = 'hello wolrd' >>> my_str.rjust(30,'-') '-------------------hello wolrd' # ljust (字符串填充,传递两个参数,第一个为输出多少位,第二个为占位符,字符串左对齐) >>> my_str = 'hello world' >>> my_str.ljust(30,'-') 'hello world-------------------'
-
在python中字符串属于不可变类型,强行改变值得会报错
View Code
-
-
练习题(网上摘抄)
# 写代码,有如下变量,请按照要求实现每个功能 (共6分,每小题各0.5分)
name = " aleX"
# 1) 移除 name 变量对应的值两边的空格,并输出处理结果
# 2) 判断 name 变量对应的值是否以 "al" 开头,并输出结果
# 3) 判断 name 变量对应的值是否以 "X" 结尾,并输出结果
# 4) 将 name 变量对应的值中的 “l” 替换为 “p”,并输出结果
# 5) 将 name 变量对应的值根据 “l” 分割,并输出结果。
# 6) 将 name 变量对应的值变大写,并输出结果
# 7) 将 name 变量对应的值变小写,并输出结果
# 8) 请输出 name 变量对应的值的第 2 个字符?
# 9) 请输出 name 变量对应的值的前 3 个字符?
# 10) 请输出 name 变量对应的值的后 2 个字符?
# 11) 请输出 name 变量对应的值中 “e” 所在索引位置?
# 12) 获取子序列,去掉最后一个字符。如: python 则获取 pytho。 -