The meaning of the occurrence of string data
Mastering the definition and characteristics of strings
Ability to master string common operations and learn about other factory methods
Definition and creation of strings
A string is an ordered set of characters that is used to store and represent basic textual information, and the content between the quotes "" is a string.
How to create
string = "quoted in quotation marks."
Characteristics:
The character set is defined in order from left to right, and the following table is accessed sequentially from 0, ordered
Tips:
1. String single and double quotation marks cannot cancel the meaning of special characters, if it is necessary to cancel their special meanings:
Then precede it with R for example: Name = R ' I\THF '
2. Unicode string with R must precede r, such as name = Ur ' I\THF '
Common operations:
#索引
name = ' Yan Xia ting Yu '
Print (Name[0])
>>:y
Print (Name[-1])
>>:u
Print (Name[-2])
>>:y
Str.index (Element) #查找元素 if there is a return index, there is no error.
Print (Name.index (' e '))
Riase Throw exception: Valueerror:substring not found
Str.find (Element)
Print (Name.find (' e ')) #元素存在返回 index, no return-1
Does not throw an exception error
Print (Name.find (' y '))
Print (Name.strip ()) #移除两边的空格
Print (Name.lstrip ()) #移除左边的空格
Print (Name.rstrip ()) #移除右边空格
Name = ' ****** yan Xia ting yu ********* '
Print (Name.strip ("*")) #移除两边的 * characters
#字符串的长度
Print (name.__len__ ()) #一般不用, which is how the Len () method is implemented
>>:35
Print (len (name))
>>:35
Name = ' ****** yan Xia ting yu ********* '
#字符串的替换
Print (Name.replace (' * ', ' 8 ')) #不指定则全部替换
>>:888888 Yan Xia Ting yu 888888888
Print (Name.replace (' * ', ' 8 ', 8)) #指定替换个数
>>:888888 Yan Xia Ting yu 88*******
Print (Name.replace (' * ', ' 8 ',-8))
>>;888888 Yan Xia Ting yu 888888888
Print (Name.replace (' * ', ' 8 ',-1))
>>:888888 Yan Xia Ting yu 888888888
#切片
name = ' Yanxiatingyu '
Print (name[0:])
Print (name[0::])
Print (Name[0:7]) #顾头不顾尾
Print (Name[0:7:2]) #步长为2
Print (Name[4::2]) #步长为2
Print (name[::-1]) #反向步长 #取反
Print (Name[:-7:-1])
Print (Name[-2:-7:-1])
Print (Name[-1:-7:-1])
>>:yanxiatingyu
>>:yanxiatingyu
>>:yanxiat
>>:ynit
>>:itny
>>:uygnitaixnay
>>:uygnit
>>:ygnit
>>:uygnit
#一些比较有用的函数
name = ' Yanxiatingyuyanxiatingyu '
Upper_name= ' Yanxiatingyu '
Print (Name.capitalize ())
Print (Upper_name.casefold ())
Print (Name.center (8 ')) #第一个参数设置宽度, second: The complete character:
Print (Name.center) #不写默认为空格 # The original string is centered, not enough to complete the character
Print (Name.count (' y ', 0,7)) #统计字符出现的次数, Start_int_index,end_int_index set the lookup range
Print (Name.count (' y ')) #不设定不限制: Default range is all
Yanxiatingyuyanxiatingyu
Yanxiatingyu
Yanxiatingyuyanxiatingyu
Yanxiatingyuyanxiatingyu
1
4
Import Hashlib
HS=HASHLIB.MD5 ()
Hs.update (Name.encode (' Utf-8 '))
Print (Hs.hexdigest ())
>>:ecbf46328d68cca2a29e618186efd377
#字符串的格式输出
#format的三种玩法res = ' {} {} {} '. Format (' Egon ', ' Male ') res= ' {1} ' {0} {1} '. Format (' Egon ', ', ' Male ') res= ' {name} {age} {sex} '. Format (sex= ' Male ', name= ' Egon ', age=18)
#字符串的拼接
I took it out alone, and I was interested to see it.
# in and not in member operations
If ' Y ' in name:
Print (' presence ')
If ' Y ' isn't in name:
Print (' not present ')
#1, Strip,lstrip,rstrip#2, lower,upper#3, startswith,endswith#4, format three ways to play # #, Split,rsplit#6, Join#7, Replace#8, IsDigit
#stripname = ' *egon** ' Print (Name.strip (' * ')) print (Name.lstrip (' * ')) print (Name.rstrip (' * ')) #lower, Uppername= ' Egon ' Print (Name.lower ()) print (Name.upper ()) #startswith, endswithname= ' alex_sb ' Print (Name.endswith (' SB ')) print ( Name.startswith (' Alex ')) #splitname = ' Root:x:0:0::/root:/bin/bash ' Print (Name.split (': ')) #默认分隔符为空格name = ' c:/a/b/c/ D.txt ' #只想拿到顶级目录print (name.split ('/', 1)) name= ' a|b|c ' Print (Name.rsplit (' | ', 1)) #从右开始切分
>>:[' a|b ', ' C ']
#isdigit: You can judge bytes and Unicode types, which are most commonly used to determine whether a character is a "number" method
Print (Age.isdigit ())
Print (Name.capitalize ())
#首字母大写
Print (Name.swapcase ())
#全部大写
Print (Name.istitle ())
#判断首字母是否大写
#find, Rfind,index,rindex,countname= ' Egon say hello ' Print (name.find (' O ', 1,3)) #顾头不顾尾, not found then return-1 will not error, found then display index # Print ( Name.index (' e ', 2,4)) #同上, but could not find the error print (Name.count (' e ', 1,3)) #顾头不顾尾, if you do not specify a range then find all #center,ljust,rjust,zfillname= ' Egon ' Print (Name.center (+, '-')) print (Name.ljust (+, ' * ')) print (Name.rjust (+, ' * ')) print (Name.zfill ()) #用0填充 # Expandtabsname= ' Egon\thello ' Print (name) print (Name.expandtabs (1)) #captalize, Swapcase,titleprint (name.capitalize ()) #首字母大写print (Name.swapcase ()) #大小写翻转msg = ' Egon say hi ' Print (Msg.title ()) #每个单词的首字母大写 #is number series # Python3 ' 4 ' in num1=b Bytesnum2=u ' 4 ' #unicode, python3 without adding U is unicodenum3= ' four ' #中文数字num4 = ' Ⅳ ' #罗马数字 #isdigt:bytes,unicodeprint (Num1.isdigit ( )) #Trueprint (Num2.isdigit ()) #Trueprint (Num3.isdigit ()) #Falseprint (Num4.isdigit ()) #False #isdecimal:uncicode# Bytes Type no Isdecimal method print (Num2.isdecimal ()) #Trueprint (Num3.isdecimal ()) #Falseprint (Num4.isdecimal ()) #False # Isnumberic:unicode, Chinese numerals, Roman numerals #bytes type no Isnumberic method print (Num2.isnumeric ()) #Trueprint (Num3.isnumeric ()) #TrueprinT (Num4.isnumeric ()) #True # Three cannot determine floating-point num5= ' 4.3 ' Print (num5.isdigit ()) print (Num5.isdecimal ()) Summary: The most common is isdigit, can judge bytes and Unicode type, this is also the most common digital application scenario if you want to judge Chinese numbers or Roman numerals, you need to use the IsNumeric "#is其他print (' ===> ') name = ' egon123 ' Print (Name.isalnum ()) #字符串由字母或数字组成print (Name.isalpha ()) #字符串只由字母组成print (Name.isidentifier ()) Print ( Name.islower ()) print (Name.isupper ()) print (Name.isspace ()) print (Name.istitle ())
Python basic Data type string