字符操作

a = 'hello world'

print('字符串长度是',len(a))
print('单词首字母大写',a.title())
print('字符串变大写',a.upper())
print('字符串是不是大写',a.isupper())
print('字符串是不是hello开头',a.startswith('hello'))

  %s  %d  %f

#%s

a ='helloworld'
print('a=%s'% a)
print('a=%15s'% a) #15代表位数,不够左侧补空格
print('a=%.5s' % a) #.代表截取字符,数字代表位数
print('a=%15.5s'% a) #.前面仍然代表位数,注意补齐
print('a=%-15s'%a) #-代表右侧空格补齐
print('a=%*.*s'%(15,5,a)) #参数赋值

  

#%d
b = 123
print('b=%d'%b)
print('b=%5d'%b) #不够位数,左侧补齐
print('b=%.5d'%b) #.后不够位数,左侧补0
print('b=%-5d'%b) #右侧补空格
print('b=%6.5d'%b)#output= 00123,先用0补齐5位,后空格补齐六位
print('b=%-.5d'%b) #outout=00123,-和.一起时无用,-放在.后会报错
print('b=%06.5d'%b) #.前面仍是补0 output=000123
print('b=%*.*d'%(9,5,b))

  

#%f

c = 12.345
print('c=%f'%c) #output=12.345000
print('c=%10f'%c) #10位,小数点算一位,左侧补空格
print('c=%3.f'%c) #.后无数字代表只取前面整数,output= 12
print('c=%10.4f'%c) # output=    12.3450
print('c=%-7.4f'%c) #output=12.3450
print('c=%*.*f'%(10,5,c)) #output=  12.34500

  

posted @ 2020-07-21 10:58  hyeonsori  阅读(161)  评论(0)    收藏  举报