【笔记】Python学习记录

Python学习记录

Hello World

老调调了,如何在终端输出信息呢?

python 复制代码
print("Hello World")
bash 复制代码
Hello World

变量

变量命名遵从代码变量命名通则,几乎所有的语言都是这一套规则,就算不是的也可以用这一套规则。

  • 仅包含字母、数字和下划线。不能以数字开头。
  • 不能包含空格。
  • 不要用关键字和函数名。
  • 要简短且有效。
  • 慎用小写字母l和大写字母O。

就目前而言,应使用小写的 Python 变量名。虽然在变量名中使用大写字母 不会导致错误,但大写字母在变量名中有特殊的含义,这将在本书后面讨论。

python 复制代码
message = "hello world"
print(message)

简单数据类型

字符串

可以用单引号,也可以用双引号。

大小写转换

title upper lower
首字母大写 全大写 全小写
python 复制代码
name = "wang leiLEI"
print(name)
print(name.title())
print(name.upper())
print(name.lower())
bash 复制代码
wang leiLEI
Wang LeiLEI
WANG LEILEI
wang leilei

插入变量

python 复制代码
h = "hello"
w = "world"
message = f"{h} {w}"
print(message)

message = f"{h.title()} {w.upper()}"
print(message)
bash 复制代码
hello world
Hello WORLD

Tab和Enter

Tab制表符 Enter回车
\t \n
python 复制代码
print("hello")
print("\thello") # Tab
print("hel\nlo") # Enter
bash 复制代码
hello
	hello
hello
hel
lo

删除前后空格

前空格 后空格 前后空格
lstrip rstrip strip
python 复制代码
message = " hello world "
print(message)
print(message.rstrip())
print(message)
print(message.lstrip())
print(message)
print(message.strip())
print(message)

上图·就是一个空格。可以看出这3个删除方法是不会影响原字符串的。

删除前后缀

前缀 后缀
removeprefix removesuffix
python 复制代码
url = "http://www.baidu.com"
print(url.removeprefix("http://"))
print(url)
print(url.removesuffix(".com"))
bash 复制代码
www.baidu.com
http://www.baidu.com
http://www.baidu

删除方法不会影响原字符串。

相关推荐
aiweker2 分钟前
python数据分析(九):Pandas 分类数据(Categorical Data)处理
python·数据分析·pandas
世事如云有卷舒8 分钟前
u-boot学习笔记(三)
笔记·学习
5649839 分钟前
爬虫学习————开始
爬虫·学习
Nina_7171 小时前
Day 15 训练
python
橙色小博2 小时前
Python中的re库详细用法与代码解析
linux·python·正则表达式·php·re
满怀10152 小时前
【库(Library)、包(Package)和模块(Module)解析】
python
北温凉2 小时前
【学习笔记】机器学习(Machine Learning) | 第五章(2)| 分类与逻辑回归
笔记·机器学习
zhojiew2 小时前
learning ray之ray强化学习/超参调优和数据处理
python·ai
xiaocao_10232 小时前
手机上使用的记录笔记的软件推荐哪一款
笔记·便签软件
bryant_meng2 小时前
【python】Calculate the Angle of a Triangle
开发语言·python·算法