第02周学习提升建议:【python安装、变量、输入输出、流程、循环】--【第四篇】字符串和数字
一、字符串
删除右边的空白 str.rstrip() ,删除左边的空白 str.lstrip(),如果删除左边+右边的空格 str.strip()
#字符串大小写
myName = 'zhang san'
print(myName)
print(myName.upper())
print(myName.lower())
#把字符串修改为标题形式,首字母大写比如zhang san,都修改为Zhang San
print(myName.title())
#字符串拼接
hisName = 'li si'
print('hello! '+myName)
print('hello! '+hisName)
#删除空白
myName1 = ' wang wu '
print(myName1)
print(myName1.rstrip()+'.')
print(myName1.lstrip())
print(myName1.strip())
二、数字
#整数、浮点数、相互转换
a = 9
b = '2'
c = 2.0
print(a/int(b))
print(a%c)
print(a/float(b))
print(str(a)+b)
字符串格式化及格式化输出
#字符串 % s
print("Hello,%s" % "world")
#整数 % d
print("My age is %d" % 20)
#浮点数 % f
print("My age is %f" % 20)
格式化输出
name = input("what is your name:")
age = int(input("tell me you age:"))
job = input("job:")
salary = input("salary:")
info = '''
---- info of %s ------
name:%s
age:%d
job:%s
salary:%s''' % (name, name, age, job, salary)
print(info)
info2 = '''
---- info of {_name} ------
name:{_name}
age:{_age}
job:{_job}
salary:{_salary}
'''.format(_name=name, _age=age, _job=job, _salary=salary)
print(info2)
info3 = '''
---- info of {0} ------
name:{0}
age:{1}
job:{2}
salary:{3}
'''.format(name, age, job, salary)
print(info3)
执行后输入内容:
what is your name:zs
tell me you age:27
job:tester
salary:15000
---- info of zs ------
name:zs
age:27
job:tester
salary:15000
---- info of zs ------
name:zs
age:27
job:tester
salary:15000
---- info of zs ------
name:zs
age:27
job:tester
salary:15000