numeric types and String types
The 1.bin () function converts decimal into a binary
The 2.OCT () function converts decimal into octal
The 3.hex () function converts decimal into hexadecimal
Hexadecimal representation: 0-9 a B c d E F
4. Characteristics of numeric types:
Only one value can be stored
Once defined, cannot be changed
Direct access
Categories: integers, booleans, floating-point, plural
5. String type
Quotation marks contain string types
s1= ' Hello world ' s= "Hello World"
s2= "" "Hello World" ""
s3= ' Hello World '
Single-lead double-lead no difference
6. Common Operations for strings
Strip () remove whitespace, or remove other characters
Slipt () split, separated by a space by default. can also be split with other characters
Len () Length slice: such as print (X[1:3]) is also Gu Tou regardless of the tail
Print (X[0:5:2]) #0 2 4
Capitalize () Capitalize first letter
Center () Center Display for example: x= ' Hello ' Print (X.center (30, ' # '))
Count (): Count, Gu Tou regardless of the tail, count the number of characters, the space is also counted as a character
EndsWith () at what end
Satrtswith () Start with what
Find () finds the index position of the character, if it is negative, indicates that the lookup failed
Index () indexes
The difference between find () and index (), such as:
Format () string formatting
1.msg= ' name:{},age:{},sex:{} '
Print (Msg.format (' Haiyan ', 18, female))
2.msg= ' name:{0},age:{1},sex:{0} '
Print (Msg.format (' aaaaaa ', ' bbbbbb '))
3.msg= ' Name:{x},age:{y,sex:{z} '
Print (Msg.format (x= ' Haiyan ', y= ', z= ' women '))
IsDigit () Determine if it is a number
Islower () to determine whether it is all lowercase
Isupper () to determine if it is all uppercase
Lower () convert all to lowercase
Upper () Convert all to uppercase
Isspace () Determine if it is all spaces
Istitle () to determine if it is a title (capitalized in the first letter)
Swapcase () uppercase and lowercase letters flipped
Join () connection
Repalce () Replace
msg= ' Hello Alex '
Print (Msg.replace (' e '), ' A ', 1)
Print (Msg.replace (' e '), ' A ', 2)
Ljust () Align Left
x= ' ABC ' Print (X.ljust (10, ' * '))
Some methods of string formatting and string
1.%s,%d
Example 1:name= ' Egon '
Age=20
Print ("My name is%s my age is%s"% (name,age)) #%s can accept both a string and a number
Print (' My name is%s my age is%d '% (name,age)) #%d can only accept numbers
Example 2: Display of user information
1 while True:2 name=input ("Name:") 3 Age=input ("Age:") 4 sex=input ("Sex:") 5 height=input ("Height:") ) 6 msg= "7 ------------%s Info-----------8 name:%s 9 age:%s10 sex:%s11 height:%s12 ------------------------------ "% (name,name,age,sex,heigth) print (msg)
The results of the operation are as follows:
2. String methods
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611 7118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 1581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971 98199200201202203204205 |
# name=‘egon‘ #name=str(‘egon‘)
# print(type(name)) #优先掌握
#1.移除空白strip
# msg=‘ hello ‘
# print(msg)
# print(msg.strip())
# 移除‘*’
# msg=‘***hello*********‘
# msg=msg.strip(‘*‘)
# print(msg)
#移除左边的
# print(msg.lstrip(‘*‘))
#移除右边的
# print(msg.rstrip(‘*‘)) #用处
while
True
:
name
=
input
(
‘user: ‘
).strip()
password
=
input
(
‘password: ‘
).strip()
if
name
=
=
‘egon‘
and
password
=
=
‘123‘
:
print
(
‘login successfull‘
)
#切分split
# info=‘root:x:0:0::/root:/bin/bash‘
# print(info[0]+info[1]+info[2]+info[3])
# user_l=info.split(‘:‘)
# print(user_l[0])
# msg=‘hello world egon say hahah‘
# print(msg.split()) #默认以空格作为分隔符
#cmd=‘download|xhp.mov|3000‘
# cmd_l=cmd.split(‘|‘)
# print(cmd_l[1])
# print(cmd_l[0])
# print(cmd.split(‘|‘,1))
#用处
while
True
:
cmd
=
input
(
‘>>: ‘
).strip()
if len
(cmd)
=
=
0
:
continue
cmd_l
=
cmd.split()
print
(
‘命令是:%s 命令的参数是:%s‘
%
(cmd_l[
0
],cmd_l[
1
]))
#长度len
# print(len(‘hell 123‘))
#索引
# 切片:切出子字符串
# msg=‘hello world‘
# print(msg[1:3]) #1 2
# print(msg[1:4]) #1 2 3
# 掌握部分
oldboy_age
=
84
while
True
:
age
=
input
(
‘>>: ‘
).strip()
if
len
(age)
=
=
0
:
continue
if
age.isdigit():
age
=
int
(age)
else
:
print
(
‘must be int‘
)
#startswith,endswith
# name=‘alex_SB‘
# print(name.endswith(‘SB‘))
# print(name.startswith(‘alex‘))
#replace
# name=‘alex say :i have one tesla,my name is alex‘
# print(name.replace(‘alex‘,‘SB‘,1))
# print(‘my name is %s my age is %s my sex is %s‘ %(‘egon‘,18,‘male‘))
# print(‘my name is {} my age is {} my sex is {}‘.format(‘egon‘,18,‘male‘))
# print(‘my name is {0} my age is {1} my sex is {0}:
{
2
}
‘.format(‘
egon
‘,18,‘
male‘))
# print(‘my name is {name} my age is {age} my sex is {sex}‘.format(
# sex=‘male‘,
# age=18,
# name=‘egon‘)) # name=‘goee say hello‘
# # print(name.find(‘S‘,1,3)) #顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
# # print(name.index(‘S‘)) #同上,但是找不到会报错
#
# print(name.count(‘S‘,1,5)) #顾头不顾尾,如果不指定范围则查找所有
#join
# info=‘root:x:0:0::/root:/bin/bash‘
# print(info.split(‘:‘))
# l=[‘root‘, ‘x‘, ‘0‘, ‘0‘, ‘‘, ‘/root‘, ‘/bin/bash‘]
# print(‘:‘.join(l))
#lower,upper
# name=‘eGon‘
# print(name.lower())
# print(name.upper())
#了解部分
#expandtabs
# name=‘egon\thello‘
# print(name)
# print(name.expandtabs(1))
#center,ljust,rjust,zfill
# name=‘egon‘
# # print(name.center(30,‘-‘))
# print(name.ljust(30,‘*‘))
# print(name.rjust(30,‘*‘))
# print(name.zfill(50)) #用0填充
#captalize,swapcase,title
# name=‘eGon‘
# print(name.capitalize()) #首字母大写,其余部分小写
# print(name.swapcase()) #大小写翻转
# msg=‘egon say hi‘
# print(msg.title()) #每个单词的首字母大写
#在python3中
num0
=
‘4‘
num1
=
b
‘4‘
#bytes
num2
=
u
‘4‘
#unicode,python3中无需加u就是unicode
num3
=
‘四‘
#中文数字
num4
=
‘Ⅳ‘
#罗马数字
#isdigt:str,bytes,unicode
# print(num0.isdigit())
# print(num1.isdigit())
# print(num2.isdigit())
# print(num3.isdigit())
# print(num4.isdigit())
#isdecimal:str,unicode
# num0=‘4‘
# num1=b‘4‘ #bytes
# num2=u‘4‘ #unicode,python3中无需加u就是unicode
# num3=‘四‘ #中文数字
# num4=‘Ⅳ‘ #罗马数字
# print(num0.isdecimal())
# # print(num1.)
# print(num2.isdecimal())
# print(num3.isdecimal())
# print(num4.isdecimal())
#isnumeric:str,unicode,中文,罗马
# num0=‘4‘
# num1=b‘4‘ #bytes
# num2=u‘4‘ #unicode,python3中无需加u就是unicode
# num3=‘四‘ #中文数字
# num4=‘Ⅳ‘ #罗马数字
#
# print(num0.isnumeric())
# # print(num1)
# print(num2.isnumeric())
# print(num3.isnumeric())
# print(num4.isnumeric())
#is其他
# name=‘egon123‘
# print(name.isalnum()) #字符串由字母和数字组成
# name=‘asdfasdfa sdf‘
# print(name.isalpha()) #字符串只由字母组成
#
# name=‘asdfor123‘
# print(name.isidentifier())
name
=
‘egGon‘
print
(name.islower())
# print(name.isupper())
# print(name.isspace())
name
=
‘Egon say‘
print
(name.istitle())
|
Python full stack development "second piece" Python data type