A built-in method numeric type

Built-in digital type method

Integer and floating point numeric collectively Wei

Integer built-in method (int)

1. Uses: Age rating number

2. Definitions: use int (), the purely digital string to decimal integer

age = 18  #  age = int(10)
print(type(age))

<class 'int'>

x = int('18')
print(type(x))

<class 'int'>

x = int('11.1') #报错
print(x)

3. The method of normal operation + Built: arithmetic comparison operations +

Long integer

Longs only python2 in, python3 does not exist in a long integer.

4. The stored value or a plurality of values: a value

5. orderly or disorderly: no orderly or disorderly say

Variable or immutable

id constant value of the variable, i.e., modified on the basis of the original value, for the variable data type; id value becomes variable, i.e., to re-apply a new value into the space, compared with immutable data types.

age = 18
print(id(age))
age += 1
print(id(age))

140714622350112
140714622350144

6. The variable or non-variable: immutable data type

Built-float method (float)

1. Use: height and weight Salary

2. Definitions: You can use float () method will be converted to a pure digital floating-point number.

age = 3.1  # age = float(3.1)
print(type(age))
<class 'float'>
x = float('111')
print(x)
print(type(x))
111.0
<class 'float'>
x = float('11.1')  # 报错
print(type(x))

<class 'float'>

3. Method built + common operations: arithmetic comparison operations +

4. The stored value or a plurality of values: a value

5. orderly or disorderly: no orderly or disorderly say

salary = 3.1
print(f'first:{id(salary)}')
salary = 5.1
print(f'second:{id(salary)}')

first:4423173584
second:4423173800

6. The variable or non-variable: immutable data type

String type built-in method (str)

1. Purpose: describe the nature of things, such as the person's name, a single loving, address, country, etc.

2. Definitions: Use '', '', '' '' '' "" "" "" wrapped string of characters

name = 'kang'
s1 = str(1.1)
s2 = str([1,2,3])
print(name)
print(s1)
print(s2)

<class 'str'>
<class 'str'>
<class 'str'>

3. common operations + built-in method: built-in methods and common operations are divided into priority master (today have to remember), need to have (within a week to remember), other operations (understand) in three parts.

Priority grasp

  1. By index values
  2. slice
  3. Length len
  4. Member operator in | not in
  5. Remove blank strip
  6. Segmentation split
  7. cycle

1. Press the index value (preferably only immutable)

# str索引取值
name = 'kang kang'
#       0123456.. #索引序号
print(name[0])
print(name[2])

k
n

2. Section, (care regardless of the end, may be added after step)

# 索引切片
name = 'kang kang'
#       0123456.. #索引序号

print(name[2:])  #切片2到最后
print(name[2:6]) #切片2到6
print(name[2:6:2]) #切片2-6步长为2
# 了解,步长为正从左到右;步长为负从右到左
print(name[-2:-5:-1]

the first
k
n
nak

3. length len

# str长度
name = 'kang kang'
print(len(name))

9

4. Members and not in operation in

# str成员运算
name_lis = ['kang', 'lin', 'wei']

print('lin' in name_lis)
print('wei' in name_lis)
print('haha' not in name_lis)

True
True
True

5. Remove the blank strip ()

# str移除空白strip()
name = '   kang  kang***'

print(name.strip())# strip()默认为‘ ’,并且不修改原值,新创建空间
print(name.strip('*'))

# strip()应用场景
pwd = input('password: ')  # 用户可能会手抖输入空格
if pwd.strip() == '123':
    print('密码输入成功')
  1. Segmentation split
# str切分split

info = "***kang male 19***"
print(info.split()) # 默认以空格切割字符串
print(info.split('*'))  #以*号切割
print(info.split('*',2))

[ ' He', 'male', '19 ']
[' ',' ',' ',' 19's male ',' ',' ',' ']
[' ',' ',' which male 19 * * ']

7. cycle

msg = 'hello kang'

for i in msg:
    print(i)

h
e
l
l
o

k
a
n
g

Need to know

  1. lstrip&rstrip
  2. lower&upper
  3. startswith&endswith
  4. rsplit
  5. join
  6. replace
  7. isdigit

1.lstrip () and rstrip ()

name = '**kang**'

print(name.lstrip('*'))
print(name.rstrip('*'))

which
he

2.lower () and upper ()

#str之lower和upper()

name = 'Kang'
print(name.lower()) #全变小写
print(name.upper())  #全变大写

kang
kANG

3.rsplit ()

# str之rsplit()
name = '**kang**'

print(name.rsplit('*',2))

[ 'A **', '', '']

4.startswith()和endswith()

name = 'Kang Wei'

print(name.startswith('Kang'))
print(name.endswith('wei'))

True
False

5.join()

lis = ['kang', 'haha', '19']
print('*'.join(lis)) #以*号进行拼接

kang*haha*19

6.replace()

# str值的replace
name = 'kang'
print(name.replace('n', 'l'))

kalg

7.isdigit ()

# str值的isdigit()  #判断是否为数字类型
age = '18'
print(age.isdigit()) # True  

salary = '111.1'
print(salary.isdigit()) # False

Other operations (requested information)

  1. find|rfind|index|rindex|count
  2. Center | bright | rjust | zfill
  3. expandtabs
  4. captalize | swapcase | title
  5. series is

1.find()、rfind()、index()、rindex()、count()

# str之find()、rfind()、index()、rindex()、count()
msg = 'my name is tank, tank shi sb, hha'

print(f"msg.find('tank'): {msg.find('tank')}")  # 找不到返回-1
print(f"msg.find('tank',0,3): {msg.find('tank',0,3)}")
print(f"msg.rfind('tank'): {msg.rfind('tank')}")  # 找不到返回-1
print(f"msg.index('tank'): {msg.index('tank')}")  # 找不到报错
print(f"msg.rindex('tank'): {msg.rindex('tank')}")  # 找不到报错
      

print(f"msg.count('tank'): {msg.count('tank')}")
msg.find('tank'): 11
msg.find('tank',0,3): -1
msg.rfind('tank'): 17
msg.index('tank'): 11
msg.rindex('tank'): 17
msg.count('tank'): 2

2.center (), light (), rjust (), zfill ()

# str之center()、ljust()、rjust()、zfill()
print(f"'info nick'.center(50,'*'): {'info nick'.center(50,'*')}")
print(f"'info nick'.ljust(50,'*'): {'info nick'.ljust(50,'*')}")
print(f"'info nick'.rjust(50,'*'): {'info nick'.rjust(50,'*')}")
print(f"'info nick'.zfill(50): {'info nick'.zfill(50)}")  # 默认用0填充
'info nick'.center(50,'*'): ********************info nick*********************
'info nick'.ljust(50,'*'): info nick*****************************************
'info nick'.rjust(50,'*'): *****************************************info nick
'info nick'.zfill(50): 00000000000000000000000000000000000000000info nick

3.expandtabs()

# str之expandtabs()
print(f"a\\tb\\tc: %s"%('a\tb\tc\t'))  # 默认制表符8个空格
print(f"'a\\tb\\tc'.expandtabs(32): %s"%('a\tb\tc\t'.expandtabs(32)))
a\tb\tc: a  b   c   
'a\tb\tc'.expandtabs(32): a                               b                               c                               

4.captalize () swapcase (), title ()

# str之captalize()、swapcase()、title()
name = 'nick handsome sWAPCASE'

print(f"name.capitalize(): {name.capitalize()}")
print(f"name.swapcase(): {name.swapcase()}")  # 大小写互转
print(f"name.title(): {name.title()}")
name.capitalize(): Nick handsome swapcase
name.swapcase(): NICK HANDSOME Swapcase
name.title(): Nick Handsome Swapcase

5.is digital hierarchy (just to tell you that, in addition to determining whether the digital Chinese digital future use isdigit () can be)

  • isdecimal (): check whether the string value contains decimal character, if it returns True, otherwise False.
  • isdigit (): If the string contains only digit returns True, otherwise False.
  • isnumeric (): If the string contains only numeric characters, it returns True, otherwise False.
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

#"〇","零","一","壱","二","弐","三","参","四","五","六","七","八","九","十","廿","卅","卌","百","千","万","万","亿"

Other 6.is

  • isalnum (): If there is at least one character string and all the characters are letters or numbers returns True, otherwise False.
  • isalpha (): If there is at least one character string and all the characters are the letters return True, otherwise False.
  • islower (): If the string contains only at least one of alphanumeric characters, and all of these (case-sensitive) characters are lowercase, returns True, otherwise False.
  • isspace (): If the string contains only blank, returns True, otherwise False
  • isupper (): If the string contains at least one of alphanumeric characters, and all of these (case-sensitive) characters are uppercase, it returns True, otherwise False.
  • istitle (): If the string is the type of title (see title ()), it returns True, otherwise False.

4. The stored value or a plurality of values: a value

The ordered or unordered: As long as the index, are ordered, so the string is ordered.

name = 'nick'
print(f'first:{id(name)}')
name = 'nick handsome'
print(f'second:{id(name)}')
first:4377100160
second:4377841264

6. The variable or non-variable: immutable data type

Guess you like

Origin www.cnblogs.com/kangwy/p/11290934.html