Python 字符串 拼接替换 格式化等

1. +号拼接

注意: 拼接的是int 时会相加
name = input("Please input your name: ")
age = input("Please input your age: ")
sex = input("Please input your sex: ")

print("Information of " + name + ":" + "\n\tName:" + name + "\n\tAge:" + age + "\n\tSex:" + sex)

输出结果如下:

Information of Alex:
    Name:Alex
    Age:38
    Sex:girl

    字符串拼接直接进行相加就可以,比较容易理解,但是一定要记得,变量直接相加,不是变量就要用引号引起来,不然会出错,另外数字是要转换为字符串才能够进行相加的,这点一定要记住,不能把数字直接相加。

2. %进行拼接

"%s%s%s" % (i,j,k)

3. 使用单引号(”””)或者双引号(“”“”“”)的方式

name = input("Please input your name: ")
age = input("Please input your age: ")
sex = input("Please input your sex: ")

message = '''
Information of %s:
Name:%s
Age:%s
Sex:%s
'''%(name,name,age,sex)
print(message)

输出结果如下:

Information of Alex:
    Name:Alex
    Age:38
    Sex:girl

    使用单引号('''''')或者双引号("""""")的方式,这种方式也很方便,我们首先进行定义,把我们需要的格式进行定义,要经常尝试这几种格式的方法,这三种方式我都觉得挺好的。

python代码过长的换行

python代码换行就是每行后面加个 \



举个栗子:

time = "2017"
print "one" + "," \
+ "two" \
+ ",three" + \
"," + time



打印出来就是:

one,two,three,2017



再举一个栗子:

print "this line is toooooooooooo \
long"



打印出来:

this line is toooooooooooo long

字符串连接的N种方式总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值