zoukankan      html  css  js  c++  java
  • Python之路 day2 字符串函数

      1 #Author:ersa
      2 
      3 name = "ersa"
      4 
      5 #首字母大写capitalize()
      6 print(name.capitalize())
      7 
      8 name = "my name is ersa"
      9 #字符串中 子串 重复的次数
     10 print(name.count("a"))
     11 
     12 #center() 字符串打印输出在行中间,并指定打印长度,不够可用其他字符补充
     13 print(name.center(50,'-'))
     14 
     15 #endswith() 判断字符串以什么结尾?输出 True 或 False
     16 print(name.endswith("sa"))
     17 
     18 #expandtabs()指定 tab 键转换为多个空格
     19 name = "my 	name is ersa"
     20 print(name.expandtabs(tabsize=20))
     21 
     22 #find()查找字符串所在的位置
     23 print(name.find("y"))
     24 
     25 #字符串可以直接被当做列表使用
     26 print(name[name.find("name"):])
     27 
     28 name = "my name is {name} an i am {year} old"
     29 #format()格式化输出
     30 print(name.format(name="ersa",year=33))
     31 
     32 #format_map() 参数可以是dict
     33 print(name.format_map({'name':'ersa','year':22}))
     34 
     35 #isalnum()是否是阿拉伯数字
     36 print('123'.isalnum())  #True
     37 print('123#$'.isalnum())  #False
     38 print('ab123'.isalnum())  #True
     39 
     40 #isalpha()大小写字母
     41 print('abcA'.isalpha())  #True
     42 
     43 #isdecimal() 是否是十进制数
     44 print('123'.isdecimal())    #True
     45 
     46 #isdigit() 是否是数字
     47 print('123'.isdigit())  #True
     48 
     49 #isidentifier() 是否是合法的标识符(变量名)
     50 print('ab1'.isidentifier()) #True
     51 print('1ab1'.isidentifier()) #False
     52 
     53 #islower() 是否是小写字母
     54 print('abC'.islower())  #False
     55 
     56 #isupper() 是否大写
     57 print('My Name '.isupper())   #False
     58 
     59 
     60 #isnumeric()是否是数字
     61 print('3.3'.isnumeric())    #False
     62 print('33'.isnumeric()) #True
     63 
     64 #isspace() 是否是空格
     65 print(' '.isspace())    #True
     66 print('a '.isspace())   #False
     67 
     68 #istitle() 是否标题
     69 print('my name '.istitle())   #False
     70 print('My Name '.istitle())   #True
     71 
     72 #isprintable() 是否可打印
     73 print('a'.isprintable())    #True tty fiel,drive file不可打印
     74 
     75 #join() 把用户输入的参数当作一条命令交给os.system 来执行
     76 #import os,sys
     77 #os.system(''.join(sys.argv[1:]))
     78 
     79 print('+'.join(['1','2','3']))
     80 
     81 print(name.ljust(50,'*'))
     82 print(name.rjust(50,'-'))
     83 
     84 #大小写转换
     85 print('Ersa'.lower())
     86 print('Ersa'.upper())
     87 
     88 #去除字符串中前后的回车换行空格符
     89 print('
    Ersa'.lstrip())
     90 print('Ersa
    '.rstrip())
     91 print('     Ersa
    '.strip())
     92 
     93 print('------')
     94 
     95 #替换对应字符
     96 p = str.maketrans("abcdef","123456")
     97 print("ersa ma".translate(p))
     98 
     99 #替换
    100 print('ersa ma'.replace('a','A',1))
    101 print('ersa ma'.replace('a','A'))
    102 
    103 #查找 --> 找最右边的 对应的下标返回
    104 print('ersa ma'.rfind('a'))
    105 
    106 #指定分隔符
    107 print('ersa ma'.split('s'))
    108 print('1+2+3+4'.split('+'))
    109 print('1+2
    +3+4'.splitlines())
    110 
    111 #字符串转大写
    112 print('ersa ma'.swapcase())
    113 
    114 print('ersa ma'.title())
    115 
    116 #zfill 用0填充
    117 print('ersa ma'.zfill(10))
  • 相关阅读:
    [hdu5400 Arithmetic Sequence]预处理,容斥
    [hdu5399 Too Simple]YY
    [hdu5396 Expression]区间DP
    [hdu5392 Infoplane in Tina Town]置换的最小循环长度,最小公倍数取模,输入挂
    [bzoj2038 [2009国家集训队]小Z的袜子(hose)] 莫队算法
    [hdu1506 Largest Rectangle in a Histogram]笛卡尔树
    hdu5381 The sum of gcd]莫队算法
    [hdu5389 Zero Escape]数根的性质,DP
    [hdu5387 Clock]时钟夹角问题
    [CodeForces 300D Painting Square]DP
  • 原文地址:https://www.cnblogs.com/iersa/p/6196079.html
Copyright © 2011-2022 走看看