字符串的调用方法

 

重要:join ,split , find , strip , upper , lower , replace,   切片, 索引, len() , for循环, range()

 

## 字符串一旦创建,不可修改

## 一旦修改或者拼接, 都会造成重新生成字符串

************************************************************************************************************************************************************************************************************************

1.   capitalize() 首字母大写

test = "aLex"
v = test.capitalize()
print(v)
View Code

 2.  casefold()  .  lower()   所有字母变小写,  casefold() 使用范围更广泛,

test1 = “aLex”
test2 = "ALEX"
v1 = test1.casefold()
v2 = test2.lower()
print(v1)
print(v2)
View Code

3. center(参数)   设置总宽度,并将内容居中。(20代指总长度,空白处用 * 填空,只能用一个字符填空)

   ljust()   设置总长度 并将内容靠左

   rjust   设置总长度 并将内容靠右

test = "aLex"
v = test.center(20,"*")
print(v)
View Code

4. count(‘参数‘  ) 在字符串中寻找子序列出现的次数,可以设置寻找的起始位置和结束的位置

test ='aLexalexr'
v = test.count('ea'.2,5)
print(v)
View Code

5.  endswith()  判断以什么结尾

     startswith()  判断以什么开头

test = ‘alex’
v1 = test.endswith('ex')
v2 =test.startswith('ex')
print(v1, v2)
View Code

6. find()  从开始往后找,找到第一个之后,获取其位置,可设置寻找的起始位置和结束位置,未找到显示-1

   index() 和 find() 功能一样,只是找不到直接报错, 忽略掉

test = 'alexalex'
v =test.find('ex', 1, 8)
print(v) 
View Code

7. format()  格式化, 将一个字符串中的占位符替换为指定的值,占位符可从0开始

text = ' i am {name}, age {a}'
print(text)
v = test.format(name ='alex', a=19)
print(v)
View Code

8. format_map()  格式化,传入的值只能是  {'name':'alex', 'a':19} 这种格式

test = 'i am {name}, age {a}'
v = test.format_map({'name':'alex', 'a':19})
print(v)
View Code

9. isalnum()  字符串中是否是    字母 和 数字

    isalpha()   判断字符串中是否是   字母,汉字

test = '123fds_+'
v = test.isalnum()
print(v)
View Code

10. expandtabs.断句20

text = 'username\temail\tpassword\nhuang\txiaobin@q.com\t12545\nhuang\txiaobin@q.com\t12545'
v = test.expandtabs(20)
print(v)
View Code

 11. isdecimal   和    isdigit()  和   isnumeric() 当前输入的是否是数字,isdigit()  可以识别更多类型不支持中文数字,isnumeric()中文数字也支持,isdecimal最常用

test = ''
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)
View Code

12. isidentifier()   是否是 字母,数字,下划线: 标识符  (数字不能开头)

a = 'def'
v = a.isidentifier()
print(v)
View Code

 13. isprintable()   是否存在不可显示的字符

test = 'sjkdj\tdsk'
v = test.isprintable()
print(v)
View Code

 14. isspace()   判断是否全是空格

test = '  '
v = test.isspace()
pring(v)
View Code

15. istitle() 判断是否是标题 (首字母是大写)

   title()  首字母变大写

test = 'Return True if the string is an uppercase string, False otherwise'
v1 = test.istitle()
print(v1)

v2 = test.title()
print(v2)

v3 = v2.istitle()
print(v3)
View Code

16. join 将字符串中的每一个元素按照指定分隔符进行拼接

test = '今天不用上班'
v = '_'.join(test)
print(v)
View Code

 17. islower()  判断是否全是小写

  lower()  全转换成小写

test = 'Alex'
v1 = test.islower()
v2 = test.lower()
print(v1, v2)
View Code

18. isupper() 判断是否全是大写

      upper()  全转换成大写

test = 'Alex'
v1 = test.isupper()
v2 = test.upper()
print(v1,v2)
View Code

19. lstrip()  rstrip()   strip()   默认去除左边空白,右空白。strip()两边空白都去除

            还能去 \n , \t  加参数去字符。(加参数,先最多字符进行匹配去除)

test1 = '    Alex    '
test2 = '    Alex    '
test3 = '    Alex    '
v1 = test1.lstrip()
v2 = test2.rstrip()
v3 = test3.strip()
print(v1)
print(v2)
print(v3)
View Code

 20.  maketrans(),   translate(),      maketans()建立对应关系, translate() 对对应关系进行替换

v = 'dsdasldkjksljkajfkd;jdkslajkd'
m = str.maketrans('avdsjf' , '123587')
new_v = v.translate(m)
print(new_v)
View Code

21.  partition() , rpartition() , split() , rsplit()      partition 只能分三份,且保留分割元素。split  可定义分割个数,但分割元素不会存在,(rpartiton, rsplit   从右到左进行分割。)

  splitlines()    也是进行分割, 只能根据换行进行分割,加参数 true , false  ,是否保留换行

test = 'jfdlsjklfdjslkjflkdsjkld'
v1 = test.partition('d')
v2 = test.rpartition('d')
v3 = test.split('j',3)
print(v1,)
print(v2)
print(v3)
View Code

22. startswith() , endswith()      startswith 判断以什么开头  , endswith判断以什么结尾

test = 'backenddjk'
v1 = test.startswith('b')
v2 = test.endswith('k')
print(v1,v2)
View Code

23. swapcase()  大小写转换

test = 'ALEs'
v = test.swapcase()
print(v)
View Code

24. replace()   进行字符子序列替换,可设置替换个数

test = 'exthpleexpl'
v1 = test.replace('ex','ccc')
print(v1)
v2 = test.replace('ex','ccc',1)
print(v2)
View Code

 

 通过索引,下标获取字符串中的某个字符

 切片

 python3:  len 获取当前字符串中由几个字符组成   python2: len 中文获取是一个中文三字符

test = "博客园"
v1 = test[2]
v2 = test[0:3]
v3 = len(test)
print(v1,v2,v3)

 for循环

#for 变量名 in 字符串:
#       变量名

test = '努力学习程序语言'
for st in test:
    print(st)

 range()  帮助创建连续的数字, 通过设置步长来指定不连续

#不会立马创建,当调用时在创建
v = range(0,100,5)

for item in v:
    print(item)

 

转载于:https://www.cnblogs.com/huangxiaobin/p/10725959.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值