换行写法:
str1="Hello World!"
str2="Hello Nowcoder!"
print(str1+'\n'+str2)
print(str1,str2,sep='\n') sep是print函数的一个参数,用来定义输出数据之间的间隔符号
print(str1,'\n',str2,sep='') sep=''表示中间无任何间隔
print(f'{str1}\n{str2}')
格式化输出
name=input() -- python3 里 input() 默认接收到的是 str 类型
# 方法1
# print('I am {} and I am studying Python in Nowcoder!'.format(name))
# 方法2:位置一一对应
# print('I am {0} and I am studying Python in Nowcoder!'.format(name))
# 方法3:参数
# print('I am {A} and I am studying Python in Nowcoder!'.format(A=name))
# 方法4:文本用 %s 占位符
print('I am %s and I am studying Python in Nowcoder!'%name)
输出两位小数
|
进制转换
b:二进制;o:八进制;d:十进制;x:十六进制
转二进制 bin()
转八进制 oct()
转十进制 int()
转十六进制 hex()
格式化输出
.lower() 全部小写
.upper() 全部大写
.title() 字符串内的所有单词首字母大写
.capitalize() 字符串第一个字母大写
去除空格
.strip() 删除两边空格
.lstrip() 删除左边空格
.rstrip() 删除右边空格
.replace() 删除所有空格
.split() 先切分,""join() 再拼接
字符串格式化
Python字符串格式化 - nutix - 博客园 (cnblogs.com)
split() 通过指定分隔符对字符串进行切片