python第四课

一、运算符

+、-、*、/、%、//、==、!=、>、<、>=、<=、<>

in、not in :包含、不包含

字符串由字符构成,成员运算就可以使用in来判断成员是否与集体存在包含关系

name = 'hustar'
if 'us' in name:
    print('ok')
else:
    print('error')

 not False == True

v = 1==1  先比较再赋值,从前到后比较(如果很多逻辑运算),先非 后且 再或。

二、数据类型

字符串,str

  b = 'hustar'

  b.upper()  字符串具有的特性

capitalize首字母大写

test = "alex"
v = test.capitalize()
print(v)

lower全部变小写、swapcase:大小写转换

test = "aLex"
v1 = test.casefold()  # 能做未知的特殊字符之间的大小写转换,比lower更全面。
v2 = test.lower()
print(v1, v2)

center设置宽度并将内容居中,用 指定字符填充,rjust,ljust分别是左填充和右填充,zfill:用0填充。

test = 'alex'
v3 = test.center(20, '*')  # fillchar是一个字符,默认位None
print(v3)

计算某个字符/子序列出现的次数

test = 'alexalex'
num = test.count('ex',5,8)  # 后两位参数是起始位置和结束位置
print(num)

encode和decode

endswith/startswith以什么结尾/开始的判断

test = 'alex'
v = test.endswith('ex')
print(v)
v = test.startswith('a')
print(v)

find 从开始往后找,遇到第一个获取其位置,起始下标为0

test = 'alexalex'
v = test.find('ex', 5, 8)
print(v) 

format格式化,将字符串中的占位符替换为指定的值

test = 'i am {name},age {a}'  # {0} {1}
print(test)
v = test.format(name='alex', a=19)  # ('alex',19)
print(v)
v = test.format_map({"name":"alex", "age":19}) # 传入字典,结果一样
print(v)

isalnum判断字符串中是否只是包含字母和数字,isnumeric、isalpha类似同理

test = '12341234afg'
v = test.isalnum()
print(v)

isupper和islower 判断字符传是否全是大写或小写

test3 = 'Abcd'
test4 = 'AB'
v1 = test3.isupper()
v2 = test4.isupper()
v3 = test3.islower()
v4 = test4.islower()
print(v1, v2, v3, v4)

istitle 判断每一个单词是否只有首字母大写

test3 = 'Hustar'
test4 = 'adx'
v5 = test3.istitle()
v6 = test4.istitle()
print(v5, v6)

isspace

Return True if all characters in B are whitespace

and there is at least one character in B, False otherwise.

如果B中的所有字符都是空格,并且B中至少有一个字符则返回True,否则为False。

test = " a"
v = test.isspace()
print(v)

isdigit、isdecimal、isnumeric

B.isdigit() -> bool

Return True if all characters in B are digits

and there is at least one character in B, False otherwise.

如果B中的所有字符都是数字,则返回True

并且B中至少有一个字符,否则为False。

test1 = " 12"
v = test1.isdigit()
print(v)
v = test1.isdecimal()
print(v)

isascii

Return True if B is empty or all characters in B are ASCII,

False otherwise.

如果B为空或B中的所有字符均为ASCII,则返回True,

否则为False。

test = ""
v = test.isascii()
print(v)

isidentifier

Return True if the string is a valid Python identifier, False otherwise.

Call keyword.iskeyword(s) to test whether string s is a reserved identifier,

such as "def" or "class".

如果字符串是有效的Python标识符,则返回True,否则返回False。

调用Keyword.isKeyword(S)以测试字符串s是否是保留标识符,

例如“def”或“class”。

test1 = '12k'
v = test1.isidentifier()
print(v)

isprintable

Return True if the string is printable, False otherwise.

A string is printable if all of its characters are considered printable in

repr() or if it is empty.

如果字符串可打印,则返回True,否则返回False。

如果字符串的所有字符在中都被认为是可打印的,则该字符串是可打印的

repr()或它是否为空。

test1 = 'sfd
sldf'
v = test1.isprintable()
print(v)

insert

Insert a single item into the bytearray before the given index.

index

The index where the value is to be inserted.

item

The item to be inserted.

在字节数组中给定索引之前插入一项。

索引 要插入值的索引。

项目 要插入的项。

!!!没有使用出来

★★★★★join

串联任意数量的字节/字节数组对象。其方法被调用的字节数组被插入到每对之间。结果作为新的bytearray对象返回。

test1 = 'alex'
test2 = 'hustar'
v = test1.join(test2)
print(v)

ltrip,rstrip,strip分别就是左,右,两边去除参数包含的字符,一旦遇到不匹配后就停止strip,优先最多匹配。

Strip leading bytes contained in the argument.

If the argument is omitted or None, strip leading ASCII whitespace.

去掉参数中包含的前导字节。

如果省略参数或无参数,则去掉前导ASCII空格。

test3 = ' r j k l '
v = test3.lstrip()
print(len(v), v)

v = test3.strip() # 参数是被判断只要包含于其中就可以匹配去除,正则匹配。
print(v)

v = test3.rstrip()
print(v)

test4 = 'abacdbaefa'
v = test4.strip('a')
print(v)

replace

test = "alexalexalex"
v = test.replace('ex', 'bb', 1)  # 最后一个参数表示替换前n个
print(v)

 index

B.index(sub[, start[, end]]) -> int

Return the lowest index in B where subsection sub is found,

such that sub is contained within B[start,end]. Optional

arguments start and end are interpreted as in slice notation.

Raises ValueError when the subsection is not found.

B.index(sub[,start[,end]])->int

返回B中找到SUB的最低索引,

使得SUB包含在B[开始,结束]内。可选

参数start和end被解释为切片表示法。

找不到该子部分时引发ValueError。

test1 = '123'
v = test1.index('1', 0, 2)
print(v)

expandtabs

Return a copy where all tab characters are expanded using spaces.

If tabsize is not given, a tab size of 8 characters is assumed.

返回一个副本,其中所有制表符都使用空格展开。

如果未指定制表符大小,则假定制表符大小为8个字符。

test = "username	email	password
laiying	ying@q.com	123
laiying	ying@q.com	123
laiying	ying@q.com	123"
v = test.expandtabs(20)
print(v)

maketrans和translate

根据对应关系对指定对象进行替换。

t = '你是风儿我是沙'
test1 = '去你妈的风和傻'
test2 = '你好呀,沙沙树'
m = str.maketrans(t, test1)
v = test2.translate(m)
print(v)

partition、rpartition

使用给定的分隔符将字符串划分为三个部分。

这将在字符串中搜索分隔符。如果找到分隔物,

返回包含分隔符(分隔符)之前的部分的3元组

它本身,以及它后面的部分。

如果找不到分隔符,则返回包含原始字符串的3元组

和两个空字符串

test = '我爱你爱你'
v = test.partition('')
print(v)

split、rsplit、splitlines

使用这个方法可以使对象按照参数进行分割,并且拿不到匹配到的参数

test = 'aslkdjfhshfjksbf'
v = test.split('j', 1)  # rsplit同理
print(v)
test = 'saldfjhal
fjlskdfjlsj
sfdf'
v = test.splitlines(True)
print(v)

数字,int

  a=123

  a.bit_length()  数字具有的特性

  数字的魔法

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

指定编码方式  

num = '0011'
v = int(num, base=8)
print(type(v), v)

num.bit_length()  # 当前数字至少用几位二进制来表示

num = 10
r = num.bit_length()
print(r)

 

原文地址:https://www.cnblogs.com/hustar/p/14015164.html