4591566 发表于 2018-8-13 09:15:29

python基础1-字符串

#需要掌握:移除,移除左边,右边  
#1.strip,lstrip,rstrip
  
name='....yaoruixue....'
  
print(name.strip('.'))
  
print(name.lstrip('.'))
  
print(name.rstrip('.'))
  
yaoruixue
  
yaoruixue....
  
....yaoruixue
  
##lower,upper(字符串大小写)
  
print('YaoRuiXue'.lower())
  
print('YaoRuiXue'.upper())
  
yaoruixue
  
YAORUIXUE
  
##startswith,endswith 以什么开头结尾
  
msg='yao rui xue'
  
print(msg.startswith('yao'))
  
print(msg.endswith('xue'))
  
print(msg.startswith('a'))
  
True
  
True
  
False
  
format的三种玩法,格式化字符串的方式,从python2.6新增的。(官方推荐用的方式,%方式将会在后面的版本被淘汰)
  
print ('my name is %s my age is %d' %('yao',23))
  
print('my name is {} my age is {}'.format('yao',23))
  
    my name is yao my age is 23
  
print('{0} {1} {0}'.format('yao',23)) ##通过位置参数,第一个参数是0,然后1。。
  
    yao 23 yao
  
print('my name is {name} my age is {age}'.format(age=19,name='yao'))
  
names={'name':'Kevin','name2':'Tom'}
  
print 'hello {names}i am {names}'.format(names=names) ##通过字典key
  
    hello Kevini am Tom
  
#replace替换
  
msg='hello world my name is yao'
  
msg=msg.replace('yao','xue',1)
  
print(msg)
  

  
#isdigit判断是否为整形
  
age=input('>>: ').strip()
  
if age.isdigit():
  
    age=int(age)
  
else:
  
    print("必须输入数字")
  

  
#了解
  
#1、find,rfind,index,rindex,count
  
#2、center,ljust,rjust,zfill
  
#3、expandtabs
  
#4、captalize,swapcase,title
  
#5、is数字系列
  
#6、is其他
页: [1]
查看完整版本: python基础1-字符串