SEVEN

python

A digital type of built-in method

1.1 Built-shaping method (int)

1.1.1 Use

Integer of age, Id, rating and so on.

1.1.2 definitions

May be used int (), the purely digital shaping into decimal string type.

age = 20
age = int(10)
print(type(age))
​
#<class 'int'>

1.1.3 Common Operations

+ Arithmetic comparison operations.

1.1.4 orderliness

There is no ordering

1.1.5 stored value

Single Value

1.1.6 Variability

Immutable

1.2 Method built float (float)

1.2.1 Use

Payroll, height, weight and other data decimals.

1.2.2 definitions

You can float () method of the string to a purely digital floating-point number.

age = 3.1
age = float(3.1)
print(type(age))
​
#<class 'float'>

1.2.3 Common Operations

+ Arithmetic comparison operations.

1.2.4 orderliness

There is no ordering

1.2.5 stored value

Single Value

1.2.6 Variability

Immutable

Second, the method of the built-in string type

2.1 string type built-in method

2.1.1 Use

Describe the nature of things, such as the person's name, a single loving, address, country, etc.

2.1.2 Defining

Use '', '', '' '' '', "" "" "" wrapped a string of characters

  • u'unicode ': unicode string encoded
  • b'101 ': binary coded string
  • r '\ n': original string, that is to say '\ n' which is an ordinary two characters, line feed and meaningless

2.1.3 ordered or unordered

As long as there is an index, it is ordered, so the string is ordered

2.1.4 certificates of deposit value or multi-value

Single Value

2.1.5 variable or invariable

Immutable

2.2 Common operations

2.2.1 Key

1. index value
mm = 'hello nick'

print(f'索引为6:{mm[6]}')
print(f'索引为-3:{mm[-3]}')

#索引为6: n
#索引为-3:i
2. Slice
mm = 'hello nick'

print(f'切片3到最后:{m[3:]}')
print(f'切片3-8:{mm[3:8]}')
print(f'切片3-8,步长为2:{mm[3:8:2]}')
print(f'切片3-最后,步长为2:{mm[3::2]}')

print('\n**了解知识点**')
print(f'切片所有: {mm[:]}')
print(f'反转所有: {mm[::-1]}')
print(f'切片-5--2: {mm[-5:-2:1]}')
print(f'切片-2--5: {mm[-2:-5:-1]}'
      
      
#切片3-最后: lo nick
#切片3-8: lo ni
#切片3-8,步长为2: l i
#切片3-最后,步长为2: l ik

#**了解知识点**
#切片所有: hello nick
#反转所有: kcin olleh
#切片-5--2:  ni
#切片-2--5: cin  
3. length len
mm = 'hello nick'
print(len(mm))

#10
4. members in or not in operation
mm = 'my name is nick, nick handsome'

print(f"'nick' in mm:{'nick' in mm}")
print(f"'jason' not in mm:{'jason' not in mm}")
print(f"not 'jason' in mm:{not 'jason' in mm}")

#'nick' in msg: True
#'jason' not in msg: True
#not 'jason' in msg: True
5. Remove the blank strip ()
name = '&&&n ick'

print(f"name.strip('&'):{name.strip('&')"})
print(f"name:{name}")

pwd = input('password: ')
if pwd.strip() == '123':
    print('密码输入成功')
    
print(f"'*-& nick+'.strip('*-&+'):{'*-& nick+'.strip('*-& +')}")   

#name.strip('&'): n ick
#name: &&&n ick
#password: 123   
#密码输入成功
#'*-& nick+'.strip('*-& +'): nick
6. Segmentation split
info = 'nick:male:19'
info_list1 = info.split(':')
info_list2 = info.split(':',1)

print(f'info_list1:{info_list1}')
print(f'info_list2:{info_list2}')

#info_list1:['nick', 'male', '19']
#info_list2:['nick', 'male:19']
7. cycle
mm = 'hello nick'
for i in mm:
    print(i)
    
#h
#e
#l
#l
#o
#n
#i
#c
#k

2.2.2 master

1.lstrip () and rstrip ()
name = '&&nick&&'
print(f"{name.lstrip('&')}")
print(f"{name.rstrip('&')}")

#nick&&
#&&nick
2.lower () and upper ()
name = 'Nick Chen'
print(f'{name.lower()}')
print(f'{name.upper()}')
      
#nick chen
#NICK CHEN
3.startswith( )和endswith( )
name = 'Nick Chen'
print(f'{name.startswith("Nick")}')
print(f'{name.endswith("chen")}')

#True
#False
4.rsplit ()
info = 'nick:male:19'
print(f"{info.rsplit(':'),1}")
      
#['nick:male','19']    
5.join( )
lis = [1,2,'19']
print(f"{':'.join(lis)}")

#报错 数字和字符串不可拼接

lis = ['nick','male','19']
print(f"{':'.join(lis)}")

#nick:male:19
6.replace( )
name = 'nick shuai'
print(f"{name.replace('shuai','handsome')}")

#nick handsome
7.isdigit ()
salary = '111'
print(salary.isdigit())

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

#True
#False
age = input('age: ')
if age.isdigit():
    age = int(age)
    
    if age < 18:
        print('小姐姐:)')
    else:
        print('阿姨好:)')
else:
    print(f'你的年龄是这个{age}?')
    
#结果一:
age: 18 
阿姨好:)
#结果二:
age: 啊?
你的年龄是这个啊??   

2.2.3 for

1.find( )、rfind( )、index( )、rindex( )、count( )
mm = 'my name is tank, tank shi sb, hha'
print(f"{mm.find('tank')}")
print(f"{mm.find('tank',0,3)}") #从0开始索引,3结束索引,找不到返回-1
print(f"{mm.rfind('tank')}")
print(f"{mm.index('tank')}")
print(f"{mm.rindex('tank')}")
print(f"{mm.count('tank')}")

#11
#-1
#17
#11
#17
#2

Note: find can not find return -1, index can not find a direct error

2.center (), light (), rjust (), zfill ()
print(f"{'info nick'.center(50,'*')}")  #放置中央
print(f"{'info nick'.ljust(50,'*')}")   #调整*居左
print(f"{'info nick'.rjust(50,'*')}")   #调整*居右
print(f"{'info nick'.zfill(50)}")  #填充0居右

********************info nick*********************
info nick*****************************************
*****************************************info nick
00000000000000000000000000000000000000000info nick
3.expandtabs( )
print(f"a\\tb\\tc: %s" %('a\tb\tc\t'))
print(f"'a\\tb\\tc'.expandtabs(32): %s" %('a\tb\tc\t'.eapandtabs(32))
      
#a\tb\tc: a  b   c   
#'a\tb\tc'.expandtabs(32):a                   b                    c                    

Note: '' applied '\ T', 'fail before the command \ n', etc., become a common string

4.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()}")  #所有单词首字母大写

Nick handsome sWAPCASE
NICK HANDSOME Swapcase
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. (important)
  • isnumeric (): If the string contains only numeric characters, it returns True, otherwise False.
num = "1"  #unicode
num.isdigit()   # True

num = "1" # 全角
num.isdigit()   # True

num = b"1" # byte
num.isdigit()   # True

num = "IV" # 罗马数字
num.isdigit()   # True

num = "四" # 汉字
num.isdigit()   # False

===================
isdigit()
True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字
False: 汉字数字
Error: 无

================
import unicodedata

unicodedata.digit("2")   # 2

unicodedata.digit("2")   # 2

unicodedata.digit(b"3")   # TypeError: must be str, not bytes

unicodedata.digit("Ⅷ")   # ValueError: not a digit

unicodedata.digit("四")   # ValueError: not a digit

#"〇","零","一","壱","二","弐","三","参","四","五","六","七","八","九","十","廿","卅","卌","百","千","万","万","亿"
Other 6.is
  • salpha (): 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.
  • 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.

Guess you like

Origin www.cnblogs.com/tangceng/p/11290650.html