python符号在数字后面_Python基础之基本数据类型一《数字与字符串》

一、运算符

结果是值

算数运算

a = 10 * 10

赋值运算

a = a + 1    a+=1

结果是布尔值

比较运算

a = 1 > 5

逻辑运算

a = 1>6 or 1==1

成员运算

a = "蚊" in "郑建文"

二、基本数据类型

数字 int

1. int    将字符串转换为数字

a = "123"

print(type(a),a)   此处a的数据类型为字符串

b = int(a)

print(type(b),b)   此处b的数据类型为数字

2. bit_lenght    当前数字的二进制至少用N位表示r = age.bit_lenght()

字符串 str

1. 首字母大写

test = "aLex"v = test.capitalize()

print(v)

2. 所有变小写,casefold更牛逼,很多未知的对相应变小写

v1 = test.casefold()

print(v1)

v2 = test.lower()

print(v2)

3. 设置宽度,并将内容居中

20 代指总长度

*  填充内容,一个字符,可有可无

test = "alex"

v = test.center(20,"中")   #字符串在中间

v = test.ljust(20,"*")     #字符串在左面填充右面

v = test.rjust(20,"*")    #字符串在右面

v = test.zfill(20)      #用0填充空白

print(v)

4. 在字符串中寻找,寻找子序列的出现次数

test = "aLexalexr"

v = test.count('ex')

v = test.count('ex',5,6)   #5,6为在这个区间内找子序列的出现次数

print(v)

5. 以什么什么开始,以什么什么结尾

test = "alex"

v = test.startswith('ex')   #开始

v = test.endswith('ex')     #结尾

print(v)

6. 制表符,断句

test = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"

v = test.expandtabs(20)      #设置每个\t隔开的间距包括其中的字符总共为20

print(v)

7. 从开始往后找,找到第一个之后,获取其位置

test = "alexalex"

v = test.find('ex')   #只获取第一次出现的位置,未找到为 -1

print(v)

8. 查找索引位置, 找不到报错

test = "alexalex"

v = test.index('e')    #如有重复只获取第一个

print(v)

9. 格式化,将一个字符串中的占位符替换为指定的值

test = "i am {0}, age {1}"

v = test.format('alex',19)

print(v)

10. 格式化,传入的值 {"name": 'alex', "a": 19}

test = 'i am {name}, age {a}'

v1 = test.format(name='df',a=10)

v2 = test.format_map({"name": 'alex', "a": 19})

print(v1,v2)

11. 判断字符串中是否只包含 字母和数字

test = "123"

v = test.isalnum()

print(v)

12. 判断是否是字母,汉子

test = "as2df"

v = test.isalpha()

print(v)

13. 当前输入是否是数字

test = "二"

v1 = test.isdecimal()     #只能判断普通数字

v2 = test.isdigit()    # 类似1,②的也能判断

v3 = test.isnumeric()   #能判断各种类型数字

print(v1,v2,v3)

14. 判断不存在不可显示的字符

test = "oiuas\tdfkj"

v = test.isprintable()   # \t,\n 就为不可显示字符

print(v)

15. 判断是否全部是空格

test = ""

v = test.isspace()

print(v)

16. 判断是否是标题(每个单词首字母大写)

test = "Return True if all cased characters in S are uppercase and there is"

v = test.istitle()

v = test.title()   #转换为标题

print(v3)

17. ***** 将字符串中的每一个元素按照指定分隔符进行拼接

test = "你是风儿我是沙"

v = "_".join(test)

print(v)

18. 判断是否全部是大小写 和 转换为大小写

test = "Alex"

v = test.islower()   #是不是小写

v = test.lower()      #转换为小写

v = test.isupper()    #是不是大写

v = test.upper()    #转换为大写

print(v)

19. 移除指定字符串

test = "  xa   "

v = test.strip('xa')  #移除指定字符

v = test.lstrip()  #移除左面字符

v = test.rstrip()   #移除右面字符

print(v)

20.  对应关系替换

v = "asidufkasd;fiuadkf;adfkjalsdjf"

m = str.maketrans("aeiou", "12345")         #后面的替换前面的

new_v = v.translate(m)

print(new_v)

21. 分割为三部分

test = "testasdsddfg"

v = test.partition('s')    #以第一个出现的分割

v = test.rpartition('s')    #以右面第一个分割

print(v)

22. 用指定个数的字符分割

test = "testasdsddfg"

v = test.split('s',2)    #从左边开始

v = test.rsplit('s',2)     #从右边开始

print(v)

23. 分割,只能根据,true,false:是否保留换行

test = "asdfadfasdf\nasdfasdf\nadfasdf"

v = test.splitlines(False)

print(v)

24.  判断以xxx开头,以xxx结尾

test = "backend 1.1.1.1"

v = test.startswith('a')   #以a开头

test.endswith('a)    # 以a结尾

print(v)

25. 大小写转换

test = "aLex"

v = test.swapcase()

print(v)

26.  将指定字符串替换为指定字符串

test = "alexalexalex"

v = test.replace("ex",'bbb')

v = test.replace("ex",'bbb',2)   #数字为指定替换的个数

print(v)

###################### 7个基本魔法 ######################

1. join     将指定字符串插入到原字符串得每个字符中间,组成新的字符串

2. split    用指定个数的字符分割

3. find     从开始往后找,找到第一个之后,获取其位置

4. strip    移除指定字符串

5. upper  全部变为大写

6. lower   全部变为小写

7. replace  将指定字符串替换为指定字符串

###################### 4个灰魔法 #########################

test = "郑建文妹子有种冲我来"

一、for循环

for 变量名 in 字符串:

变量名

break

continue

index = 0

while index < len(test):

v = test[index]

print(v)

index += 1

print('=======')

for zjw in test:

print(zjw)

test = "郑建文妹子有种冲我来"

for item in test:

print(item)

break

for item in test:

continue

print(item)

二、索引,下标,获取字符串中的某一个字符

v = test[3]

print(v)

三、切片

v = test[0:-1] # 0=

print(v)

四、获取长度

Python3: len获取当前字符串中由几个字符组成

v = len(test)

print(v)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值