当前位置: 移动技术网 > IT编程>脚本编程>Python > 大蛇01 字符串操作练习

大蛇01 字符串操作练习

2019年03月24日  | 移动技术网IT编程  | 我要评论

天天酷跑糖伯虎,绣山中学,黄华华 辞职

# 1.capitalize(cap ita lize)
# 功能:字符串首字母大写
# ex1:
# name = 'max'
# print(name.capitalize())
# 回车:max

# ex2:
# name = 'max alex bob pop'
# a = name.capitalize() # print(name.capitalize())
# print(a)

# 2.casefold(case fold)
# 功能:字符串字母小写
# ex:
# name = 'max'
# print(name.casefold())
# 回车:max

# 3.center(cen ter)
# 功能:字符串居中
# ex:
# name = 'max'
# a = name.center(9,'*')
# print(a)
# 回车:***max***
# 注:
# center本身自带两个参数(1,"*")
# (1,"*")前者代表字符串回车后总长度:后者表示左右两侧要添加的符号,可为空格

# 4.count
# 功能:统计某个字符在字符串中出现的次数,或在指定区间内完成上述操作
# ex1(整间):
# name = 'leonardo wilelm dicaprio'
# a = name.count('e')
# print(a)
# 回车:2

# ex2(区间):
# name = 'leonardo wilelm dicaprio'
# a = name.count('e',1,8)
# print(a)
# 回车:1
# 注:(1,8)在python中对于范围讲究顾头不顾尾的原则

# ex3(大小写区分):
# name = 'leonardo leonardo leonardo'
# a = name.count('l')
# print(a)
# 回车:0

# name = 'leonardo leonardo leonardo'
# a = name.count('l')
# print(a)
# 回车:3

# name = 'leonardo leonardo leonardo'
# a = name.casefold().count('l')
# print(a)
# 回车:3

'''
5.encode
what the fuck???
'''

# 6.endswith(end swith)
# 功能:判断字符串是否以某个字符串结尾的,返回值为bool
# ex1:
# a = 'cyberpunk none'
# print(a.endswith('none'))
# 回车:true

# ex2:
# a = 'cyberpunk none'
# print(a.endswith('e'))
# 回车:true

# ex3:
# a = 'cyberpunk none'
# print(a.endswith('e'))
# 回车:false

'''what the fuck??
# 7.expandtabs(ex pand tabs)
# 功能:扩展标签
ex:
li = 'sw\tht'
li.expandtabs()
print(li)
'''

# 8.find
# 功能:查找查找指定字符串序列坐标,找不到回-1
# ex1:
# name = 'max'
# print(name.find('a'))
# 回车:1

# ex2:
# name = 'max'
# print(name.find('a'))
# 回车:-1

# ex3:
# name = 'max'
# print(name.find('m'))
# 回车:0

# 9.format(for mat)
# 功能:{ } { } 占位符(借鉴理解%s)
# ex1:
# speech = '{} have a {}.'
# print(speech.format('you','dog'))
# 回车:you have a dog.

# ex2:
# speech = '{} have a {}.'
# print(speech.format('i','dream'))
# 回车:i have a dream.

# 10.__contains__(con tains)
# 功能:包含,指定字符串是否被包含,返回bool
# ex1:
# speech = 'i have a dream'
# print(speech.__contains__('av'))
# 回车:true

# ex2:
# speech = 'i have a dream'
# print(speech.__contains__('t h'))
# 回车:false

# 11.index
# 功能:指数(在字符串中查找指定的字符串坐标,找不到时直接报错)
# ex1:
# name = 'max'
# print(name.index('x'))
# 回车:2

# ex1:
# name = 'max'
# print(name.index('x'))
# 回车:报错,substring not found (未找到子字符串)

# 12.'*'join()
# 功能:间接添加
# ex1:
# name ='max'
# print('@'.join(name))
# 回车:m@a@x

# ex2:
# name ='m a x'
# print('**'.join(name))
# 回车:m** **a** **x

# 13.isalnum(is al num)
# 功能:检查判断字符串是否包含字母数字字符,回车bool
# ex1:
# id = '85757max'
# print(id.isalnum())

# ex2:
# id = 'max'
# print(id.isalnum())

# 14.isalpha(is al pha)alpha字母表
# 功能:检测字符串是否包含字母组成,回车bool
# ex1.
# name = 'max'
# print(name.isalpha())
# 回车:true

# ex2.
# id = '89757max'
# print(id.isalpha())
# 回车:false

# ex3.
# id = '2.2'
# print(id.isalpha())
# 回车:false

# 15.lower
# 功能:将所有字母转换成小写
# ex1.
# name = 'max'
# print(name.lower())
# 回车:max

# 16.isdecimal(is decimal)
# 功能:判断字符串是否为十进制,回车bool
# ex1.
# id = '101'
# print(id.isdecimal())
# 回车:true

# ex2.
# id = 'max101'
# print(id.isdecimal())
# 回车:false

# ex3.
# id = '54.1'
# print(id.isdecimal())
# 回车:false

# 17.isdigit(is digit)
# 功能:判断字符串是否仅为数字组成,回车bool
# ex1.
# id = '555'
# print(id.isdigit())
# 回车:true

# ex2.
# id = 'max555'
# print(id.isdigit())
# 回车:false

# 18.isidentifier(isi dent ifi er)
# 功能:判断字符串是否为字母开头,回车bool
# ex1.
# name = 'max'.isidentifier()
# print(name)
# 回车:true

# ex2.
# name = '0123max'
# a = name.isidentifier()
# print(a)
# 回车:false

# 19.isnumeric(is num eric)
# 功能:判断字符串是否只由数字组成,和isdigit区别????,回车bool
# ex1.
# id = '222000'.isnumeric()
# print(id)
# 回车'true'

# ex2.
# id = 'abc 000'.isnumeric()
# print(id)
# 回车:false

# 20.isprintable (is print able)
# 功能:判断字符串是否为能够打印的,回车bool
# ex1.
# id = "\tmax".isprintable()
# print(id)
# 回车:false

# ex2.
# id = "max".isprintable()
# # print(id)
# 回车:true

# 21.isspace(is space)
# 功能:判断字符串是否仅为空格,回车bool
# ex1.
# id = ' '.isspace()
# print(id)
# 回车:true

# ex2.
# id = ' max'.isspace()
# print(id)
# 回车:false

# 22.istitle(is title)
# 功能:判断字符串每节是否首字母大写,回车bool
# ex1.
# name = 'max'.istitle()
# print(name)
# 回车:true

# ex2.
# name = 'ace max'.istitle()
# print(name)
# 回车:true

# ex3.
# name = 'ace max'.istitle()
# print(name)
# 回车:false

# ex4.
# name = 'max'.capitalize().istitle()
# print(name)
# 回车:true

# 23.isupper(is upper)
# 功能:判断字符串是否都为大写字母,回车bool
# ex1.
# name = 'max'.isupper()
# print(name)
# 回车:false

# ex2.
# name = 'max'.isupper()
# print(name)
# 回车:true

# 24.ljust
# 功能:左对齐 自带参数(步数,'填充内容')
# ex.
# name = 'max'.ljust(30,'*')
# print(name)
# 回车:max***************************

# 25.strip(rstrip 右去空 lstrip 左去空)
# 功能:去空
# ex.用户不小心输入了 空格空格 + max + 空格空格空格空格空格
# name = input('please put your username>>:( max )').strip().lower().capitalize()
# if name == 'max':
# print('happy birthday')
# 回车:happy birthday

# 注:假设输入的是空格 max 空格
# strip().lower().capitalize()
# 先将字符串去空,然后改为全部小写,然后将首字母大写
# 空格 max 空格 : max : max
# strip 不能去除中间空格 m a x

# 26.maketrans ????

# 27.partition(part ition)
# 功能:分区,分隔字符串
# ex.
# name = 'maxbobalice'.partition('bob')
# print(name)
# 回车:('max', 'bob', 'alice')

# 28.replace(re place)
# 功能:替换,代替,将字符串中需要替换的字符串替换
# ex.
# speech = 'i have a dream.'.replace('i','you need')
# print(speech)
# 回车:you need have a dream.

# 29.split
# 功能:分隔,默认空格
# ex.
# name = 'maxwang'.split('w'.upper())
# print(name)
# 回车:['max', 'ang']

# 30._add_
# 功能:在字符串结尾添加字符串
# ex.
# name = 'ma'.__add__('x'.lower())
# print(name)
# 回车:max

# 31._eq_
# 功能:判断字符串是否相等
# ex.
# a = '250'
# b = '250'
# print(a.__eq__('250'))
# print(a.__eq__(b))
# 回车:true
# 回车:true

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网