最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Python的变量和简单类型

网站源码admin3浏览0评论

Python的变量和简单类型

1、变量命名

  • 变量可以包含字母、数字、下划线
  • 变量可以以字母和下划线开头,但不能以数字开头
  • 不能将python中的关键字和函数名作为变量名
  • 慎用某些容易混淆的字母,如小写字母l和数字0容易与数字1和和字母o混淆

2、常用变量基本类型

字符串、整数、浮点

3、字符串

3.1、字符串基本输出

字符串用单引号''或者双引号"",或者三引号"""包围

代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码# 单引号
>>> print('hello')
hello

# 双引号
>>> print("hi")
hi

# 输出转义
# \n换行符; \t制表符
>>> print("First Line. \nSecod Line")
First Line
Second Line

# 原始输出,不进行转义,在字符串前添加r
>>> print(r"First Line. \nSecond Line")
First Line. \nSecond Line

# 保留字符串中的换行格式,使用三重单引号或者双引号
>>> print("""
    hello
    world
""")
(输出时这里还会有一个空行)
hello
world
)

# 三重引号后添加\,可以将第一行的空行删除
>>>print("""\
    hello
    world
""")
(输出时这里没有空行)
hello
world

3.2、字符串基本操作

'*'重复输出

代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码# * 重复输出字符串
>>> print(3 * hi + world)
hihihiworld

+ 字符串拼接

代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码# + 字符串拼接
>>> print('Py' 'thon')
Python

字符串索引

代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码# 字符串支持索引访问
>>> word = 'python'
>>> print(word[0])
p

# 0 和-0为同一索引位置
>>> print(word[-0])
p

>>>print(word[1])
y

# 负索引
# 由于0 和-0为同一个位置所以,字符串所在右边字符的第一个字符的下标为[-1]
>>> print(word[-1])
n

# 索引下标越界
# python的长度为6,正索引最大为5,负索引最小为-6,数值超过范围则会出错
>>> print(word[8])
Index Error :string index out of range

字符串区间索引-字符串截取

代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码>>> word = "python"

# 区间索引左侧闭区间,右侧为开区间
>>> print(word[0:2])
py

# 索引省略
# 省略开头索引
# 省略后默认从0开始
>>> print(word[:2])
py

# 省略结尾索引
>>> print(word[2:])
thon


# 区间索引值超过索引范围,不会报错,只是不返回值
>>> print(word[42:])

>>> print(word[9:])

字符串为immutable,不可被重新编辑

代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码>>> word = "python"
>>> word[1] = 'j'

TypeError:'str' object does not support item assignment

# 要生成不同的字符串,应新建一个字符串
>>> word2 = j + word[:2]

3.3、字符串常用方法

islower(),是否都是小写
代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码str = 'hellopython'
print(str.islower())

True
isspace(),是否包含空格
代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码str = 'hellopython'
print(str.isspace())

False
issupper(),是否都是大写
代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码str = 'hellopython'
print(str.issupper())

Fase
lower(),转换为小写
代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码str = 'HelloPython'
print(str)

hellopython
upper(),转换为大写
代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码str = 'HelloPython'
print(str.upper())

HELLOPYTHON
len(),获取字符串的长度
代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码str = 'hellopython'
print(len(str))

11
removeprefix(),移除指定前缀

如果指定的前缀不存在,得到的则是原始字符

代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码str = 'hellopython'
print(str.removeprefix('on'))

htllopyth
removesuffix(),移除指定后缀

如果指定后缀不存在,则返回原字符串

代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码str = 'hellopython'
print(str.removesuffix('he'))

llopython
count(),计算指定字符出现的频次
代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码str = 'hellopython'
print(str.count('ll', 0, 12))


1
endwith(),是否以指定字符结尾
代码语言:javascript代码运行次数:0运行复制
python 代码解读复制代码str = 'hellopython'
print(str.endwith('B'))

False

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论