Python 语言学习要点记录1-num,string

Python 是支持中文作为变量名的

高 = 20
宽 = 15
print(高 * 宽)     #300

在 Jupyter notebook 中显示变量的值的区别

直接用变量显示结果

a = 5
b = 6.5
c = a*b
c  
#显示结果为32.5

再增加几行代码

a = 5
b = 6.5
c = a*b
c  
d = 12
d
#显示结果12

使用print()显示结果

a = 5
b = 6.5
c = a*b
print(c) 
#显示结果为32.5
a = 5
b = 6.5
c = a*b
print(c) 
d = 12
print(d) 
#显示结果
32.5
12

结论:直接使用变量来显示结果,就只会显示最后一处变量的值。print()则会显示每一处变量的值。

字符串

双引号里可以包含单引号,或者单引号里包含双引号

"this is a 'red' apple "
'his is a "red" apple '

需要显示一些特殊字符就需要用 进行转义

'I don't like it'
""Ok", he replied "
#结果 "Ok", he replied

print('d:dataresource')
#结果 esource
print('d:dataresource')
#结果 d:dataresource

注意,在字符串中,必须一起使用,否则就会发生错误

如果不行这么复杂,可以用源字符串,即在字符串前加一个字符r

print(r'd:dataresource')
#结果 d:dataresource

当然在源字符串中,结尾处也一样不能单独加一个 ,会报错。

使用三引号显示多行字符串

print('''
      item one
      item two
      item three
     ''')
#结果
      
      item one
      item two
      item three

如果想让某一行与下一行显示在一行中,可以在这一行结尾加一个

print('''
      item one
      item two
      item three
     ''')
#结果
     
      item one      item two
      item three

使用 + 号字符串连接,使用 * 重复字符串内容

"one" * 3
3 * 'one'
#结果都是  oneoneone

用 () 折行定义长字符串

str = ("Python is a programming language that lets you work quickly"
    "and integrate systems more effectively.")
str
#结果 'Python is a programming language that lets you work quicklyand integrate systems more effectively.'

可以通过索引获取字符串中的字符

str = 'python'
str[1]  #y
str[-1] #n
str[-3] #h
str[1:3] #yt
str[]:2 #py

展开阅读全文

页面更新:2024-02-28

标签:引号   字符串   中文   变量   结尾   要点   字符   索引   结论   区别   定义   语言

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top