python基础(二)- 字符串
一、运算符
1、结果是值:
算术运算
赋值运算
2、结果是布尔值:
比较运算:> < == >= <= != <>
逻辑运算:and or not
成员运算:in
身份运算:用于比较连个对象的存储单元(内存地址),
is 判断两个标识符是否引用自一个对象,如果相同则为True ==> id(a) == id(b)
a = "abc" b = "abc" a is b ==> True a=12 b=12 a is b ==> True ============== a = [1,2,3] b = [1,2,3] a is b ==> False
#逻辑运算
#没有括号 按照顺序从左往右运算 #and运算 左右为真结果才为真;左面为真,结果取决于右面,最终结果取右面值; # or 运算 左右一个值为真结果为真;左面为真,结果就为真,值取左值即可;左面为假,不管右面值真假,都取右面值
# 混合运算 and优先
位运算:二进制
二、基本数据类型
1、数字 int
- int
#将字符串转换为数字
a = "123"
print(type(a),a)
b = int(a)
print(type(b),b)
num = "0011"
v = int(num, base=16)
print(v)
- bit_lenght
# 当前数字的二进制位数,至少用n位表示
r = age.bit_length()
2、字符串 str
方法:
# test = "aLex"
# 首字母大写
# v = test.capitalize()
# print(v)
# 所有变小写,casefold更牛逼,很多未知的对相应变小写
# v1 = test.casefold()
# print(v1)
# v2 = test.lower()
# print(v2)
# 设置宽度,并将内容居中
# 20 代指总长度
# * 空白未知填充,一个字符,可有可无
# v = test.center(20,"中")
# print(v)
# 去字符串中寻找,寻找子序列的出现次数
# test = "aLexalexr"
# v = test.count('ex')
# print(v)
# test = "aLexalexr"
# v = test.count('ex',5,6)
# print(v)
# 以什么什么结尾
# 以什么什么开始
# test = "alex"
# v = test.endswith('ex')
# v = test.startswith('ex')
# print(v)
# 从开始往后找,找到第一个之后,获取其未知
# > 或 >=
# test = "alexalex"
# 未找到 -1
# v = test.find('ex')
# print(v)
# index找不到,报错 忽略
# test = "alexalex"
# v = test.index('8')
# print(v)
def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__ """ S.split(sep=None, maxsplit=-1) -> list of strings sep为分割标记,maxsplit为分割次数,结果为列表,不包含分割符 print('abcaac'.split('c',1)) ['ab', 'aac'] print('abcaac'.split('d',1)) #['abcaac'] ------------------------- def partition(self, sep): # real signature unknown; restored from __doc__ """ 以sep为分割,将S分成head,sep,tail三部分 S.partition(sep) -> (head, sep, tail) Search for the separator sep in S, and return the part before it, the separator itself, and the part after it. If the separator is not found, return S and two empty strings. 没找到分割符,返回两个空字符串 """ pass print('abcaac'.partition('a')) # ('', 'a', 'bcaac') print('abcaac'.partition('d')) # ('abcaac', '', '')
格式化输出 形式一. >>> print('{0}{1}{0}'.format('a','b')) aba 形式二:(必须一一对应) >>> print('{}{}{}'.format('a','b')) Traceback (most recent call last): File "<input>", line 1, in <module> IndexError: tuple index out of range >>> print('{}{}'.format('a','b')) ab 形式三: >>> print('{name}--{age}'.format(age=12,name='lhf')) lhf--12 S.format(*args, **kwargs) -> str
def replace(self, old, new, count=None): # real signature unknown; restored from __doc__ """ S.replace(old, new[, count]) -> str Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. """ return "" print('aaaaa'.replace('a','1',2)) #11aaa
def join(self, iterable): # real signature unknown; restored from __doc__ """ S.join(iterable) -> str Return a string which is the concatenation of the strings in the iterable. The separator between elements is S. """ return "" #对序列进行操作(分别使用' '与':'作为分隔符) >>> seq1 = ['hello','good','boy','doiido'] >>> print ' '.join(seq1) hello good boy doiido >>> print ':'.join(seq1) hello:good:boy:doiido #对字符串进行操作 >>> seq2 = "hello good boy doiido" >>> print ':'.join(seq2) h:e:l:l:o: :g:o:o:d: :b:o:y: :d:o:i:i:d:o #对元组进行操作 >>> seq3 = ('hello','good','boy','doiido') >>> print ':'.join(seq3) hello:good:boy:doiido #对字典进行操作 >>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4} >>> print ':'.join(seq4) boy:good:doiido:hello
num = "1" #unicode num.isdigit() # True num.isdecimal() # True num.isnumeric() # True num = "1" # 全角 num.isdigit() # True num.isdecimal() # True num.isnumeric() # True num = b"1" # byte num.isdigit() # True num.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal' num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric' num = "IV" # 罗马数字 num.isdigit() # True num.isdecimal() # False num.isnumeric() # True num = "四" # 汉字 num.isdigit() # False num.isdecimal() # False num.isnumeric() # True =================== isdigit() True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字 False: 汉字数字 Error: 无 isdecimal() True: Unicode数字,,全角数字(双字节) False: 罗马数字,汉字数字 Error: byte数字(单字节) isnumeric() True: Unicode数字,全角数字(双字节),罗马数字,汉字数字 False: 无 Error: byte数字(单字节) ================ import unicodedata unicodedata.digit("2") # 2 unicodedata.decimal("2") # 2 unicodedata.numeric("2") # 2.0 unicodedata.digit("2") # 2 unicodedata.decimal("2") # 2 unicodedata.numeric("2") # 2.0 unicodedata.digit(b"3") # TypeError: must be str, not bytes unicodedata.decimal(b"3") # TypeError: must be str, not bytes unicodedata.numeric(b"3") # TypeError: must be str, not bytes unicodedata.digit("Ⅷ") # ValueError: not a digit unicodedata.decimal("Ⅷ") # ValueError: not a decimal unicodedata.numeric("Ⅷ") # 8.0 unicodedata.digit("四") # ValueError: not a digit unicodedata.decimal("四") # ValueError: not a decimal unicodedata.numeric("四") # 4.0 #"〇","零","一","壱","二","弐","三","参","四","五","六","七","八","九","十","廿","卅","卌","百","千","万","万","亿" python中str函数isdigit、isdecimal、isnumeric的区别
# 格式化,将一个字符串中的占位符替换为指定的值
# test = 'i am {name}, age {a}'
# print(test)
# v = test.format(name='alex',a=19)
# print(v)
# test = 'i am {0}, age {1}'
# print(test)
# v = test.format('alex',19)
# print(v)
# 格式化,传入的值 {"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})
# 字符串中是否只包含 字母和数字
# test = "123"
# v = test.isalnum()
# print(v)