码迷,mamicode.com
首页 > 编程语言 > 详细

python路-字符

时间:2018-10-11 19:38:40      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:lower   lex   首字母   hello   输出   pac   art   元素   统计   

字符                      

  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))

 

python路-字符

标签:lower   lex   首字母   hello   输出   pac   art   元素   统计   

原文地址:https://www.cnblogs.com/5014sy/p/9774497.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!