Python编程 从入门到实践-2变量下

 笔记出处(学习UP主视频记录) https://www.bilibili.com/video/av35698354?p=3

2.3.4 删除空白

favorite_language = 'python '
print (favorite_language.rstrip())

python

favorite_language = ' python'
print (favorite_language.lstrip())

python

2.3.5 使用字符串时避免语法错误

message = 'One of Python's strength is its diverse community.'
print (message)

message = 'One of Python's strength is its diverse community.'
^
SyntaxError: invalid syntax

message = "One of Python's strength is its diverse community."
print (message)

One of Python's strength is its diverse community.

2.4.1 整数

print (2+3)

5

print (3/2)

1.5

print (3 ** 2)

9

2.4.2 浮点数

print (0.1 + 0.1)

0.2

print (0.2 + 0.1)

0.30000000000000004

2.4.3 使用函数str()避免类型错误

age = 23
message = "Happy " + age + "rd Sirthday!"
print (message)

message = "Happy " + age + "rd Sirthday!"
TypeError: must be str, not int

age = 23
message = "Happy " + str(age) + "rd Sirthday!"
print (message)

Happy 23rd Sirthday!

2.5.1 如何编写注释

#向大家问好
print ("Hello Python world!")

Hello Python world!

原文地址:https://www.cnblogs.com/nxopen2018/p/12467662.html