开始跟着视频学python,第二天mark【字符串拼接】

今天第二天,从第30分钟开始看。

course = 'python for beginners'
course2 = "python's for beginners"   #为了python's  前后都用了双引号
course3 = 'python for "beginners"' 
print(course)
print(course2)
print(course3)

注意上面第二句为了python’s 前后都用了双引号。

如果想输入很多话的话可以用‘’‘

course = '''
hi moon 
here an email for you
i love you
bye
'''
print(course)

索引

course = 'python for beginners'
#         01234567
print(course[0])    #第一个,p
print(course[6])   #空格
print(course[0:3])    # 是0,1,2 (pyt) 没有h  额
print(course[0:])
print(course[3:])   #从3开始,h
print(course[:5])   #01234都有 ,pytho
another = course[:]   #复制了
another1 = course   #这个也行
print(another)
print(another1)

输出为

p
 
pyt
python for beginners
hon for beginners
pytho
python for beginners
python for beginners

问题来了,如果

name = 'jennifer'
print(name[1:-2])

会输出什么呢?
我猜的是ejr,错了。答案是:

ennif

这个

name = 'jennifer'
print(name[1:0])
print(name[2:0])   #都没有输出结果

拼接

 first = 'jhon'
last = 'smith'
message = first + '[' + last + ']  is  a coder'
print(message)

输出:

jhon[smith]  is  a coder

上面这种不够直观,读代码时。
另一种方式如下

扫描二维码关注公众号,回复: 11284950 查看本文章
first = 'jhon'
last = 'smith'
message = first + '[' + last + ']  is  a coder'
msg = f'{first}[{last}] is a code'    #这里用了一个f'',format(格式)  {}里面的内容被替换了
print(message)
print(msg)

输出如下

jhon[smith]  is  a coder
jhon [smith] is a code

小结:利用f,和{},在字符串中动态地插入值。
mark一下,视频位置:41:00
python教程2019版 6小时完全入门 并且达到能开发网站的能力 目前最好的python教程 (含中文翻译)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili

猜你喜欢

转载自blog.csdn.net/weixin_42944682/article/details/105301819