Basic data types and built-in method (number, string)

A: int type

1. Role: description of age, identity card number

2. Definition:

age = 10 # age=int(10)

3. Type Conversion

3.1 pure digital converted into a string of int

res=int('100111')
print(res,type(res))
100111 <class 'int'>

3.2 (understand)

2.2.1 turn into other binary decimal

Decimal -> Binary

print(bin(11)) # 0b1011

10 hex -> octal

print(oct(11)) # 0o13

10 hex -> Hex

print(hex(11)) # 0xb

3.2.2 Other converted into its decimal system

Binary -> Decimal

print(int('0b1011',2)) # 11

print(int('0b1011',2)) # 11

Binary -> octal

print(int('0o13',8)) # 11

Binary -> hexadecimal

print(int('0xb',16)) # 11

Two: float type

1, the role: for describing the payroll, height, weight, etc.

2, the definition of

salary=3.1 # salary=float(3.1)

3, type conversion

res=float("3.1")
print(res,type(res))

4, using

int and float do not need to have built-in methods
of their use is relatively math operations +

Three: string type

1. Role: used to describe a certain state, such as a person's name, appearance

2, the definition of

# msg='hello' # msg=str('msg')
# print(type(msg))

3, type conversion

str can be any other type strings are transformed into

# res=str({'a':1})
# print(res,type(res))
{'a': 1} <class 'str'>

4, using: built-in methods

4.1 Priority grasp

4.1.1, according to the index value (forward + reverse take take): can only take

msg='hello world'

Take forward

msg='hello world'
print(msg[0])
print(msg[5])
h
 是一个空格

Reverse Take

print(msg[-1])
d

Can only take

msg[0]='H'     直接报错,字符串是不可变的
 File "C:/Users/Administrator/Desktop/3.10/03 字符串类型.py", line 22, in <module>
    msg[0]='H'
TypeError: 'str' object does not support item assignment

4.1.2 Slice: expand the application index, a copy of a substring from a string of large

msg='hello world'

Care regardless of the end

res=msg[0:5] #  从0开始4结束
print(res)
hello

Steps

res=msg[0:5:2] # 0 2 4
print(res) # hlo

Reverse steps (understanding)

res=msg[5:0:-1]
print(res) #  olle
 olle

res=msg[:] # res=msg[0:11]  
print(res)
hello world

res=msg[::-1] # 把字符串倒过来
print(res)
dlrow olleh

4.1.3 length len

msg='hello world'
print(len(msg))
11

4.1.4, members of the operations in and not in

Determining whether there is a sub-string within a string of large

print("alex" in "alex is sb")
print("alex" not in "alex is sb")  # 不推荐使用
print(not "alex" in "alex is sb") 
True
False
False

4.1.5, left and right sides of the symbol string is removed strip

Remove the default space

msg='      egon      '
res=msg.strip()
print(msg) # 不会改变原值
print(res) # 是产生了新值
      egon      
egon

Other characters removed

msg='****egon****'
print(msg.strip('*'))
egon

msg='****e*****gon****'
print(msg.strip('*'))
e*****gon  了解:strip只取两边,不去中间

application

inp_user=input('your name>>: ').strip() # inp_user=" egon"
inp_pwd=input('your password>>: ').strip()
if inp_user == 'egon' and inp_pwd == '123':
    print('登录成功')
else:
    print('账号密码错误')

4.1.6, segmentation split: a string to be segmented according to some separators, give a list of

The default separator is a space

info='egon 18 male'
res=info.split()
print(res)
['egon', '18', 'male']

Specify the delimiter

info='egon:18:male'
res=info.split(':')
print(res)

Specifies the number of separated (understand)

info='egon:18:male'
res=info.split(':',1)
print(res)

4.1.7, cycling

info='egon:18:male'
for x in info:
    print(x)  依次输出

4.2 need to know

4.2.1、strip,lstrip,rstrip

msg='***egon****'
print(msg.strip('*'))
print(msg.lstrip('*'))
print(msg.rstrip('*'))
egon****
***egon
egon:18:male

4.2.2、lower,upper

msg='AbbbCCCC'
print(msg.lower())
print(msg.upper())
abbbcccc
ABBBCCCC

4.2.3、startswith,endswith

print("alex is sb".startswith("alex"))
print("alex is sb".endswith('sb'))
True
True

4.2.4、format

Before formatted output, see

4.2.5, split, rsplit: cut the string list

info="egon:18:male"
print(info.split(':',1)) # ["egon","18:male"]
print(info.rsplit(':',1)) # ["egon:18","male"]

4.2.6, join: the list spliced ​​into a string

l=['egon', '18', 'male']
res=":".join(l) # 按照某个分隔符号,把元素全为字符串的列表拼接成一个大字符串
print(res)
egon:18:male

4.2.7、replace

msg="you can you up no can no bb"
print(msg.replace("you","YOU",))
print(msg.replace("you","YOU",1))  只替换第一个
YOU can YOU up no can no bb
YOU can you up no can no bb

4.2.8、isdigit

Determining whether the string of digits pure

print('123'.isdigit())
print('12.3'.isdigit())
True
False

application

# age=input('请输入你的年龄:').strip()
# if age.isdigit():
#     age=int(age) # int("abbab")
#     if age > 18:
#         print('猜大了')
#     elif age < 18:
#         print('猜小了')
#     else:
#         print('才最了')
# else:
#     print('必须输入数字,傻子')

Learn 4.3

4.3.1、find,rfind,index,rindex,count

msg='hello egon hahaha'
print(msg.find('e')) # 返回要查找的字符串在大字符串中的起始索引
print(msg.find('egon'))
print(msg.index('e'))
print(msg.index('egon'))
1
6
1
6
# print(msg.find('xxx')) # 返回-1,代表找不到
# print(msg.index('xxx')) # 抛出异常

Count count

msg='hello egon hahaha egon、 egon'
print(msg.count('egon'))
3

4.3.2, center, light, rjust, zfill

print('egon'.center(50,'*'))
print('egon'.ljust(50,'*'))
print('egon'.rjust(50,'*'))
print('egon'.zfill(10))
***********************egon***********************
egon**********************************************
**********************************************egon
000000egon

4.3.3、expandtabs

msg='hello\tworld'
print(msg.expandtabs(2)) # 设置制表符代表的空格数为2
hello world

4.3.4, captalize, swapcase, title

print("hello world egon".capitalize())   第一个单词首字母大写
print("Hello WorLd EGon".swapcase())   大小写互换
print("hello world egon".title())      每个单词首个字母大写
Hello world egon
hELLO wORlD egON
Hello World Egon

4.3.5, is digital series

4.3.6, is the other

print('abc'.islower())   判断是否小写
print('ABC'.isupper())   判断是否大写
print('Hello World'.istitle())   判断每个单词首个字母是否大写
print('123123aadsf'.isalnum()) # 字符串由字母或数字组成结果为True
print('ad'.isalpha()) # 字符串由由字母组成结果为True
print('     '.isspace()) # 字符串由空格组成结果为True
print('print'.isidentifier())      字符定义规范
print('age_of_egon'.isidentifier())
print('1age_of_egon'.isidentifier())
True
True
True
True
True
True
True
True
False

B'4 = num1 '#Bytes in
num2 = u'4' # Unicode, python3 is no need to add u Unicode
num3 = 'four' # Chinese digital
num4 = 'Ⅳ' # Roman numerals

# isdigit只能识别:num1、num2
# print(num1.isdigit()) # True
# print(num2.isdigit()) # True
# print(num3.isdigit()) # False
# print(num4.isdigit()) # False



# isnumberic可以识别:num2、num3、num4
# print(num2.isnumeric()) # True
# print(num3.isnumeric()) # True
# print(num4.isnumeric()) # True

# isdecimal只能识别:num2
# print(num2.isdecimal()) # True
# print(num3.isdecimal()) # False
# print(num4.isdecimal()) # False

Guess you like

Origin www.cnblogs.com/chenyoupan/p/12457769.html