【笔记】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

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

相关推荐
一隅论数智3 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
默_笙3 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
XiHongShi20163 天前
STM32F407 RTC定时器例程,建议保存
stm32·单片机·学习
qq_426003963 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技3 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
爱吃苹果的日记本3 天前
离散数学第六课
学习·离散数学
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时3 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas