字符串的编辑方法

#String 操作

# 1 * 重复输出字符串
# print('hello'*20)

# 2 [] ,[:] 通过索引获取字符串中字符,这里和列表的切片操作是相同的,具体内容见列表
# print('helloworld'[2:])

#关键字 in
# print(123 in [23,45,123])
# print('e2l' in 'hello')

# 4 %   格式字符串
# print('alex is a good teacher')
# print('%s is a good teacher'%'alex')

#5
# a='123'
# b='abc'
# d='44'
# # # c=a+b
# # # print(c)
# #
# c= ''.join([a,b,d])
# print(c)



# String的内置方法

# st='hello kitty {name} is {age}'
#
# print(st.count('l'))       #  统计元素个数
# print(st.capitalize())     #  首字母大写,其余全部小写
# print(st.center(50,'#'))   #  居中


Python casefold() 方法是Python3.3版本之后引入的,其效果和 lower() 方法非常相似,都可以转换字符串中所有大写字符为小写。
两者的区别是:lower() 方法只对ASCII编码,也就是‘A-Z’有效,对于其他语言(非汉语或英文)中把大写转换为小写的情况只能用 casefold() 方法。

# print(st.endswith('tty3')) #  判断是否以某个内容结尾
# print(st.startswith('he')) #  判断是否以某个内容开头
# print(st.expandtabs(tabsize=20)) #  指定转换字符串中的 tab 符号('\t')转为空格的字符数。
# print(st.find('t'))        #  查找到第一个元素,并将索引值返回,找不到返回  -1
# print(st.format(name='alex',age=37))  # 格式化输出的另一种方式   待定:?:{}
# print(st.format_map({'name':'alex','age':22}))
# print(st.index('t'))  #与find相似,但是找不到会报错
# print('asd'.isalnum())  #判断字符串是不是只由数字、字符、汉字组成,成立返回True
# print('12632178'.isdecimal())  #判断是不是一个十进制的数字
# print('1269999.uuuu'.isnumeric()) #和isdigit()函数相同判断数字是否是整型字符
# print('abc'.isidentifier())  #判断是否为一个非法变量
# print('Abc'.islower())  #判断字符是否全部是小写
# print('ABC'.isupper())  #判断字符是否全部是大写
# print('  e'.isspace())  #判断字符串是否是一个空格
# print('My title'.istitle())  #判断字符串中是否每个单词的首字母大写
# print('My tLtle'.lower())   #大写便小写
# print('My tLtle'.upper())   #小写变大写
# print('My tLtle'.swapcase())  #大小写对换
# print('My tLtle'.ljust(50,'*'))  #字符串在左边,在其右边添加字符
# print('My tLtle'.rjust(50,'*'))  #字符串在右边,在其左边添加字符
# print('\tMy tLtle\n'.strip())  #去除字符串开头、结尾中的空格、换行\n、制表符\t
# print('\tMy tLtle\n'.lstrip()) #去除字符串左边的空格、换行\n、制表符\t
# print('\tMy tLtle\n'.rstrip()) #去除字符串右边的空格、换行\n、制表符\t
# print('My title title'.replace('itle','lesson',1)) #将指定内容替换到原字符串指定位置,并且确定替换次数,默认全部替换
# print('My title title'.rfind('t'))  #从右往左查找指定字符,并返回索引值
# print('My title title'.split('i',1)) #从左往右将指定字符替换为分隔符,将字符串转化为列表,可以限制替换次数,不输入全部替换(rsplit函数为从右往左)
# print('My title title'.title())  #将字符串内所有单词按照第一个字母大写的格式输出


#摘一些重要的字符串方法
# print(st.count('l'))
# print(st.center(50,'#'))   #  居中
# print(st.startswith('he')) #  判断是否以某个内容开头
# print(st.find('t'))
# print(st.format(name='alex',age=37))  # 格式化输出的另一种方式   待定:?:{}
# print('My tLtle'.lower())
# print('My tLtle'.upper())
# print('\tMy tLtle\n'.strip())
# print('My title title'.replace('itle','lesson',1))
# print('My title title'.split('i',1))

 

posted @ 2020-06-23 14:58  sewen  Views(650)  Comments(0)    收藏  举报