python运算符与基本数据类型之字符串

1.pycharm的用法

  • mouse——general-点击选取Ctrl+mouse 选择pycharm字体可以随着鼠标滚动改变大小
  • 具体激活过程 https://blog.csdn.net/wills798/article/details/79486243

2.基本运算符

  • 算数运算符 + - * / ** % //



  • 赋值运算符

+= (count= count + 1)

%=

/=

...




  • 逻辑运算符 比较运算符 成员运算符

==

>

<

>=

<=

!= 不等于

<> 不等于

not

  • 布尔值: 真 True 假 Fals


in not in (a是否为b的子集)
结果 布尔值

name = "张三"
if "" in name:
print("ok")
else:
print("error")





3.基本数据类型
数字 int

——int 将字符串转换成数字

a ='123'
b = int(a)
a = a + b
print(type(b),b)

  


num ="0011"
y = int(num,base = 16) #base转换成16位进制
print(y,type(y))
——bit_length
#当前数字的二进制,至少用n位来表示
r = age.bit_length() 




字符串 str

——test.capitalize()#s首字母大写	
test ="alex"
v = test.capitalize()
print(v)

  


——test.casefold()更厉害 test.lower()	#变小写	
test ="alex"
v1 = test.casefold()/test.lower()
print(v1)

  


——test.center(20,"*") #放置中间 20为总长度 *为空白填充,为单字符
test ="alex"
v1 = test.center(20,"*")
print(v1)

  

——test.ljust()#放置在左边 20为总长度 *为空白填充
test = "alex "
v = test.ljust(20,"*")
print(v)

  

—— test.rjust() #放置在右边 *为空白填充
test = "alex"
v = test.rjust(20,"*")
print(v)

  


——test.count("a",5) #计算字符a在总字符串中出现的次数 5为开始寻找的起始位置
test = "alexalexr"
v = test.count('r')
print(v)

  


——test.endswith('a')#是否以"a"结尾

——test.startwith('b')#是否以“b”结尾

  


——test.find() #从开始往后找 找到第一个之后 获取其位置
test = 'alexalex'
v = test.find('ex'
print(v) 

  

——test.fotmat() #格式化,讲一个字符串中占位符替换为指定的值
test = " i am {name},age{a}"
print(test)
v = test.format(name = 'alex',a =19)
print(v)

  


——test.isalnum#字符串中是否只包含 数字
test = '123'
v = test.isalnum()
print(v)

  

——test.isalpha() #字符串中是否只包含字母
test = "isalald"
v = test.isalpha()
print(v)

  


——test.isdecimal()#判断当前输入的是否是数字
test = "124"
v1 = test.isdecimal()#十进制的小数
v2 = test.isdigit() #包含特殊符号
v3 = test.isnumeric()#包含中文
print(v1,v2,v3)

  


——test.expandtabs() #遇到\t就补充空格 \n就换行
test = "username\temail\tpassword\nzhangghao\t124"
v = test.expandtabs(20)
print(v)

  

——test.isidentifier() #数字,字母,下划线:标识符 def class
a = "def"
v = a.isidentifier()
print(v)

  


——test.isprintable() #是否存在不可显示的字符
test = "adawdawda\t"
v = test.isprintable()
print(v)

  


——test.isspace() #是否全部是空格
test = "asdawd"
v = test.isspace()
print(v)

  

——test.istitle() #是否为标题
test = "Return True If all cased characters in s are uppercase and there is"
v = test.istitle()
v1 = test.title()
print(v,v1)

  


-test.join() #将字符串中的每一个元素按照指定分隔符进行拼接 ********
test = "你是疯子我是傻子"
print(test)
t = " "
v =t.join(test)
print(v)

  


——test.islower() #将字符串中的元素变为小写
test = "Alex"
v1 = test.islower()
v2 = test.lower()
print(v1,v2 )

  

——test.upper()	#将字符串中的元素变成大写
test = "alex"
v1 = test.isupper()
v2 = test.upper()
print(v1,v2 )

  


——test.swapcase() #大小写进行转换
test = 'alex'
v = test.swapcase()
print(v)

  


——test.strip() #取出左右空白 移除\t \n 指定内容时可以移除指定字符,最多匹配项

test = "alex"
v = test.lstrip() #v = test.lstrip("al")
v1 = test.rstrip() #v1 = test.rstrip('ex')
v2 = test.lstrip() #
print(v,v1,v2)

  


——test.maketrans()	#将指定的元素进行替换
test1 = "aeiou"
test2 = "12345"
v = "awdAtasdA2dawdasdawdagsad"
m = str.maketrans('aeiou','12345')
new_V = v.translate(m)
print(v)

  


——test.partition() test.rpartition() test.split()
test = "adawasdawsdAd"
v = test.partition('s')
print(v)
v1 = test.rpartition('s')
print(v1)
v2 = test.split('s',2)
print(v2)

  

——test.splitlines() #分割 只能根据,true false 是否保留换行
test = 'sdawddsda\nwdadffdad\ndfwdadfAef\n'
v = test.splitlines(False)
print(v)

  

——test.startwith() test.endswith()
test = "backed 1.1.1.1"
v = test.startwith('a')
print(v)
v1 = test.endswith()
print(v1)

  


——test.replace() #替换魔法
test = "alexlexalex"
v = test.replace("ex",bbb,2) #将字符串中的ex替换为bbb,但是只允许替换两次
print(v)

  


##################七个基本魔法##############
join split find strip upper lower replace

###################一个深灰魔法##############
字符串一旦创建,不可修改,一旦修改或者拼接,都会造成重新生成字符串



##################灰魔法####################
len() for循环 索引 切片
#索引 下标 获取字符串中的某一个字符
test = "alex"
v = test[0]
print(v)

#切片
v = test[0:1] 范围为开区间
print(v)

#len 获取字符串的长度
v = len(test)
print(v)

猜你喜欢

转载自www.cnblogs.com/sunshineboy0307/p/11878059.html