关于字符串的常见操作函数

这是一个笔记

str1 = 'nottin.soulrn.orlog.iduna'  #字符串是不可变类型。
def mark_out_index():               #姑且称其为字符串的遍历好了
    a = len(str1)
    for i in str1:
        print(i,end='    ')
    print('')
    for j in range(0,a):
        print('%02d' %j,end='   ')
    print('')

def return_values():  #all these functions below returns a value.
    a = str1[23]     #[index],to pick out specific character
    b = str1[::]    #[start:over:step]

    c = str1.rfind('s')     #('str'),"rfind" or "find"
    e = str1.rindex('o')    #('str'),"rindex" or "index"

    g = str1.count('.',4)       #('str', times),default counts all
    d = str1.replace('.', ',',5)#('origin_str', 'new_str', times),default replaces all
    f = str1.split('.',5)       #('str', times),default splits all and returns a list
    h = '@'.join(f)             # 'str'.join(list) ,use 'str' to connect all elements in list and returns a new str

    i = str1.title()        #changes every element's initial letter to CAP
    j = str1.capitalize()   #changes first character to CAP,and doesn't work if str starts with a space
    k = str1.upper()        #changes every letter to CAP
    l = str1.lower()        #changes every letter to lower
    m = str1.strip()        #remove spaces of both sides,"lstrip" for starts_side and "rstrip" for ends_side

    n = str1.center(29,'.') #(num,'str')center the original str and add 'str' to reach 'num' characters
    o = str1.ljust(29,'.')  #(num,'str')left align and same with above.
    p = str1.rjust(29,'.')  #no need to say

    q = str1.startswith('nott') #('str'),check if str1 starts with ('str') and return bool type
    r = str1.endswith('iduna')  #('str'),check if str1 ends with ('str') and return bool type

    s = str1.isalpha()  #judge if str is not empty and composed with letter and return bool type
    t = str1.isdigit()  #judge if str is not empty and composed with integer and return bool type
    u = str1.isalnum()  #judge if str is not empty and composed with integer/letter and return bool type
    v = str1.isspace()  #judge if str is empty and return bool type

    ww = s              #change var_name to print
    print(ww)
mark_out_index()
return_values()

希望能够得到补充或者建议。
另外我这个键盘键程比较长,有的时候虽然按下去了但是并没有触发按键

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值