python中格式化输出和字母大小写转换,对齐填充方式

#格式化输出
print("ang is a good time")
str7="ong is a boy"
num=10
f=5.22313
# %d(整数站位符) %s(字符串站位符) %f(浮点数站位符)
# %f默认小数点后6位,%.3f精确到小数点后3位。默认会四舍五入
print("num=",num,"f=",f)
print("num= %d,str7=%s,f=%.9f" %(num,str7,f))
'''
#转义字符 \
将一些字符转换成有特殊含义的字符
'''
# \n换行符,
print("num= %d\nstr7=%s\nf=%.9f" %(num,str7,f))
print("ong is a good \team")#显示ong is a good \team
#如果要没特殊含义出现都是两\\出现的
print("ong is a good \\n team")#ong is a good \n team
# \' \" 避免单引号中还有引号出现错误
#print('tom is a 'good'man')#这样会报出
print('tom is a \'good\'man')
print("tom is a 'good'man")#双引号里面可以有单引号
print("tom is a \"good\"man")
#如果多行转换可以用3个单引号或者3个双引号
print("""
ong
is
good
team
""")
# \t 制表符 是4个空格
print("tom\t girl")
#如果字符中有很多字符串都需要转义,就需要加入很多\,为了化简
#python允许用r表示内部的字符串默认不转义
print(r"C:\Users\Administrator\Desktop\学习")#加r打印出C:\Users\Administrator\Desktop\学习



#eval(str)
#功能:将字符串str当成有效的表达式来求值并返回计算结果
num1=eval("123")
print(num1)#结果123
print(type(num1))#<class 'int'>eval把字符串变为整数类型,跟int方式相同
print(eval("+123"))#123
print(eval("12+3")) #15
print(int(12+3)) #15
#print(eval("12ad")) #出错


#len(str)
#返回字符串的长度(字符个数)
print(len("ong is a good"))

#lower()转换字符串中大写字母为小写字母
#格式:str.lower()
str15="ONG IS a good"
print(str15.lower()) #ong is a good
print("str15=%s" %(str15))

#upper(str)转换字符串中小写字母为大写字母
str15="ONG IS a good"
print(str15.upper()) # ONG IS A GOOD
#swapcase() 转换字符串中小写字母为大写字母,大写字母变小写字母
str15="ONG IS a good"
print(str15.swapcase()) #ong is A GOOD

#capitalize()一段文字中首字母大写,其他小写
str15="ONG IS a good"
print(str15.capitalize()) #Ong is a good

#title()每个单词的首字母大写,其他变小写
str15="ONG IS a good"
print(str15.title()) # Ong Is A Good

#center(width,fillchar)居中对齐,width为 要求宽度,fillchar是填充字符串
str15="ONG IS a good"
print(str15.center(40,"*"))#*************ONG IS a good**************

#ljust(width[,fillchar])左对齐,fillchar是填充的字符
str15="ONG IS a good"
print(str15.ljust(40))#ONG IS a good
print(str15.ljust(40),"*") #ONG IS a good *
print(str15.ljust(40,"*"))#ONG IS a good***************************

#rjust(width,fillchar)右对齐

猜你喜欢

转载自www.cnblogs.com/zlong123/p/10420641.html