字符串处理+格式化输出


字符串处理函数
>>> s='bav sj ' >>> len(s) 7 >>> str([1,2])#与eval 正好相反 '[1, 2]' >>> hex(10)#转化为16进制 '0xa' >>> oct(10)#转化为8进制 '0o12

  a = eval(input())
  b = hex(a)
  c = b[2:]
  print(b,c)

 
>>> 'wFWEGWR'.lower()#全转化为小写。.upper 为大写
'wfwegwr'
>>> 'A,B,C'.split(',')#str根据sep被分割的部分组成
['A', 'B', 'C']
>>> 'a fe a gta '.count('a')子串在原串出现的次数
3

 print(s.center(8))

 hello  #左侧1空格,右侧2空格
>>> 'python'.replace('n','123')#所有n都会被替代为123
'pytho123'

  #在一个20宽度的字符串中,让python 居中
  >>> 'python'.center(20,'=')
  '=======python======='
  >>> 'python'.center(19,'=')
  '=======python======'


 str.strip(chars)#从str 里去掉在其左侧和右侧chars中列出的字符
 >>> "= python ".strip("=n")
 ' python '

 
>>> 'rwrgt'.strip('r')
'wrgt'
>>> '1python'.strip('t')
'1python'
>>> '123456'.strip("123")
'456'

s = 'hello'
print(s.capitalize())

Hello

print(' okok   '.strip())

okok #去空格

print(s.rjust(8))

Right-justify a string, padding with spaces; prints " hello"

获得用户输入的一个正整数输入,输出该数字对应的中文字符表示。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬
0到9对应的中文字符分别是:零一二三四五六七八九
a = input()
b=[]
data = {'1':'', '2':'','3':'','4':'','5':'','6':'','7':'','8':'','9':'', '0':''};
for i in a:
     b.append(data[i])#因为1 2 3 都加了引号,所以i 为字符,那么data[0]错,data['0'] 对的
 print(b)
c = ''.join(b)#去掉格式
print(c)
62455
['', '', '', '', '']
六二四五五

a = input()
b=[]
data = ['','', '','','','','','','',''];
for i in a:
    x = eval(i)
    b.append(data[x])
c= ''.join(b)
print(c)
str.join(it)# 在it 变量除最后元素外每个元素后增加一个str
>>> ",".join("1356")#主要用于字符串的分隔
'1,3,5,6'
Unicode 字符串的编码方式
统一字符编码,覆盖几乎所有字符的编码方式
从0到1114111(0x10FFFF)空间,每个编码对应一个字符
python 字符串中每个字符都是Unicode 编码字符
chr(u)#u为Unicode 编码,返回其对应的字符
>>> chr(9800)
''
ord(x)# x 为字符,返回其对应的Unicode 编码
>>> ord('')
9800


#打印12星座
>>> for i in range(12):
    print(chr(9800+i),end='')

    
♈♉♊♋♌♍♎♏♐♑♒♓
>>> s="sgfter ' "
>>> len(s)
9>>> t = s+'efre'
>>> t
"sgfter ' efre"
>>> t*3
"sgfter ' efresgfter ' efresgfter ' efre"
>>> for  char in  name:
    print(char)
  >>> x = "123"
  >>> y="012345"
  >>> x in y#x是y的子串吗?
  True
  >>> x = "16"
  >>> x in y
  False

#今天星期几?
westr = "星期一星期二星期三星期四星期五星期六星期日"
id  = eval(input())
pos  = (id-1)*3
print(westr[pos:pos+3])

westr = "一二三四五六日"
id  = eval(input())
print("星期"+westr[id-1])

>>> "{0:=^20}".format("python")
'=======python======='
>>> "{0:=^20}".format("python")
'=======python======='
>>> "{0:*>20}".format("NOI")
'*****************NOI'
>>> "{:10}".format("feq")
'feq       '#默认填充空格,左对齐,宽度为10
s='PYTHON'
print("{0:3}".format(s))

{0:3}表示输出的宽度是3,但如果字符串超过长度3,则以字符串长度显示。PYTHON
 >>> "{0:,}".format(46532.396)
'46,532.396'
>>> "{0:,.2f}".format(1233.369)
'1,233.37'

import math
a = eval(input())
b = pow(a,0.5)
print("{0:+>30.3f}".format(b))

10
+++++++++++++++++++++++++3.162

b :二进制
c:字符
d:十进制
o:八进制
x:16进制
X:大写16进制
#进制转化
10进制转化为其他的

>>> print("{:b}".format(15))
1111
>>> print("{:o}".format(15))
17
>>> print("{:x}".format(15))
f

其他的转化为10进制

>>> int("40",16)
64
>>> int('110',2)
6
>>> int('100',8)
64

#16进制到2进制 >>> bin(0xa) '0b1010' >>>

#2进制到16进制 >>> hex(0b1001) '0x9'

>>> "{0:b},{0:c},{0:d},{0:o},{0:x},{0:X}".format(451)
'111000011,ǃ,451,703,1c3,1C3'
>>> "{0:e},{0:E},{0:f},{0:%}".format(3.14)
'3.140000e+00,3.140000E+00,3.140000,314.000000%'
a = eval(input())
print("{:-^20}".format(pow(a, 3)))#超过20位的以结果宽度为准
获得用户输入的一个整数a,计算a的平方根,保留小数点后3位,并打印输出。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬
输出结果采用宽度30个字符、右对齐输出、多余字符采用加号(+)填充。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬
如果结果超过30个字符,则以结果宽度为准。
a = eval(input())
print("{:+>30.3f}".format(pow(a,0.5)))
获得输入的一个字符串s,以字符减号(-)分割s,将其中首尾两段用加号(+)组合后输出。
a = input()
s = a.split('-')
print("{}+{}".format(s[0],s[-1]))

  a = input()
  s = a.split('-')
  print(s[0],'+',s[-1],sep='')

原文地址:https://www.cnblogs.com/tingtin/p/11620403.html