python字符串输入拼接,python字符串拼接

1、脚本

num =7

#string和int拼接

print ("there's",num,"boys" )

#string和string拼接

#(1)直接用+号

print ("there's"+" "+"boys" )

#(2)用逗号,

print ("there's","boys" )

备注:一般连接字符串用+ 而不是逗号?

因为我的目的是将两个字符串连接起来,组建成一个新的字符串。print 里的逗号其实是分隔参数的一种方式

2、执行结果

6757aca3e5c04c23c9eb526b9c3be712.png

3、脚本

# -*- coding: utf-8 -*-

Her_name = 'babi'

Her_high = 160

Her_weight = 100

print ("this is my friend",Her_name)

print ("她",Her_high,"厘米 高")

print ("she's",Her_weight,"jin")

4、执行结果

c06873d88ce22fdba445d0b19d542493.png

5、脚本

# -*- coding: utf-8 -*-

name = 'babi'

high = 160

weight = 100

print ("this is my friend %s."%name)

print ("she's %d high."%high)

print ("she's %d jin."%weight)

print ("she is",name,",",high, "cm high,and her weight is",weight )

print ("she is %s,%d cm high and %d jin"%(name, high, weight))

6、执行结果

eb8074e82f2d44c54e46072886a061ac.png

7、脚本(主要区分%s和%r;

可以换行)

# -*- coding: utf-8 -*-

name = "balabala'ha

then"

print ("this is my friend %s."%name)

print ("this is my friend %r."%name)

8、执行结果

8b87182b1a4e4e36ff3c3efc9cebb02d.png

备注:

(1)%s, %r , %d 这些符号是啥意思? 它们是一种“格式控制工具”。

它们告诉 Python 把右边的变量带到字符串中,并且把变量值放到 %s 所在的位置上

%r 就是是常有用的一个,它的含义是“不管什么都打印出来

(2)%r 和 %s 有什么不同?

%r 用来做 debug 比较好,因为它会显示变量的原始数据(raw data),而其它的符号则是用来向用户显示输出的

(3)脚本7的string变量用双引号---------因为字符串中有特殊字符 ',如果用但引号,会报错

(4)%r可以将变量的原型输出来

(5)%s输出用户一般看到的格式

(6)

可以换行

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值