002python路--字符

字符                      

  if  age.isdigit():        #字符串age是数字”12”

    age = int(age)

  else:

    exit() #退出程序

    exit(“现在我退出了”)

截取

  print('helloworld'[2:])

属于 

  print('el' in 'hello')  

格式化  

  %s   %d     %f   

  msg = '''name =%s,age =%s'''%(name,age)

连接

  d2='--'.join([a,b])      # 123-- abc

方法############

  print( st.count(内容) )      #  统计元素个数

  print( st.endswith('tty3') )  #  判断是否以某个内容结尾

  print( st.startswith('he') )  #  判断是否以某个内容开头

  print(st.find('t'))        #  查找到第一个元素,并将索引值返回

  print('My title title'.rfind('t'))

  print('\tMy tLtle\n'.strip()) #去换行符,空格

  print('\tMy tLtle\n'.lstrip())

  print('\tMy tLtle\n'.rstrip())

  print('My title title'.replace('itle','lesson',1))  #把itle替换为lesson ,替换几次

  print('My title title'.split('i',1))  #仅以第一个i为分隔符

  print( st.capitalize() )      #  首字母大写

  print('Abc'.islower())

  print('ABC'.isupper())

  print('  e'.isspace())

  print(' tLtle'.lower())

  print('tLtle'.upper())

  print('My tLtle'.swapcase())  #大写变小写,小写变大写

  print( st.center(50,'#') )    #  居中      长度50,#填充

  print('My tLtle'.ljust(50,'*'))  #左

  print('My tLtle'.rjust(50,'*')) #右

  print('My tLtle'.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('\tMy tLtle\n'.strip())

  print('My title title'.replace('itle','lesson',1))

  print('My title title'.split('i',1))

猜你喜欢

转载自www.cnblogs.com/5014sy/p/9774497.html
002