目录
2.1 运行hello_world.py时发生的情况
2.2 变量
每个变量指向一个值——与该变量想关联的信息。
在程序中,可随时修改变量的值,而Python将始终记录变量的最新值。
2.2.1 变量的命名和使用
2.2.2 如何在使用变量时避免命名错误
2.2.3 变量是标签
2.3 字符串
2.3.1 使用方法修改字符串的大小写
使用 title() 方法:首字母大写
name = "ada lovelace"
print(name.title())
#输出Ada Lovelace
使用 upper() 方法:字母全部大写
name = "Ada Lovelace"
print(name.upper())
#输出ADA LOVELACE
使用 lower() 方法:字母全部小写
name = "Ada Lovelace"
print(name.lower())
#输出ada lovelace
2.3.2 在字符串中使用变量
2.3.3 使用制表符或换行符来添加空白
2.3.4 删除空白
确保字符串右端没有空白:rstrip() 方法(这种删除办法是暂时的)
favorite_language='python '
print(favorite_language.rstrip())#输出'python'
print(favorite_language)#输出'python '
favorite_language=favorite_language.rstrip()
print(favorite_language)#输出'python'
确保字符串左端没有空白:lstrip() 方法(这种删除办法是暂时的)
favorite_language=' python'
print(favorite_language.lstrip())#输出'python'
print(favorite_language)#输出' python'
favorite_language=favorite_language.lstrip()
print(favorite_language)#输出'python'
确保字符串两端没有空白:strip() 方法(这种删除办法是暂时的)
favorite_language=' python '
print(favorite_language.strip())#输出'python'
print(favorite_language)#输出' python '
favorite_language=favorite_language.strip()
print(favorite_language)#输出'python'
2.3.5 删除前缀
使用 removeprefix() 方法(这种删除办法是暂时的)
nostarch_url='http://nostarch.com'
print(nostarch_url.removeprefix('http://'))#输出'nostarch.com'
print(nostarch_url)#输出'http://nostarch.com'
nostarch_url=nostarch_url.removeprefix('http://')
print(nostarch_url)#输出'nostarch.com'
2.3.6 如何在使用字符串时避免语法错误
2.4 数
2.4.1 整数
2.4.2 浮点数
2.4.3 整数和浮点数
1.将任意两个数相除,结果总是浮点数,即便这两个数都是整数且相除。
2.在其他任何运算中,如果一个操作数是整数,另一个操作数是浮点数,结果也总是浮点数。
2.4.4 数中的下划线
2.4.5 同时给多个变量赋值
2.4.6 常量
2.5 注释
2.5.1 如何编写注释
使用井号“#”标识。