Python基础,strings 02
移除字符字符填充字符串比较
stringObject[ start : stop : interval]
In [1]: strObj = "This is a sample string"
In [2]: index = 5
...: # Slice string to remove character at index 5
...: if len(strObj) > index:
...: strObj = strObj[0 : index : ] + strObj[index + 1 : :]
...:
In [3]: print('Modified String : ', strObj)
Modified String : This s a sample string
In [4]: strObj = "This is a sample string"
...:
...: # Slice string to remove first character
...: strObj = strObj[1 : : ]
...:
...: print('Modified String : ' , strObj)
Modified String : his is a sample string
In [5]: strObj = "This is a sample string"
...:
...: # Slice string to remove last character
...: strObj = strObj[:-1:]
...:
...: print('Modified String : ', strObj)
Modified String : This is a sample strin
Modified String : This is a sample strin
In [6]: strObj = "This is a sample string"
...:
...: start = 5
...: stop = 10
...: # Remove charactes from index 5 to 10
...: if len(strObj) > stop :
...: strObj = strObj[0: start:] + strObj[stop + 1::]
...:
...: print('Modified String : ', strObj)
Modified String : This ample string
In [7]: numStr = "5"
...: print('Original String :', numStr)
...:
...: # Left pad the string with 0 to make it's length 4
...: numStr = numStr.zfill(4)
...:
...: print('Updated String :' , numStr)
Original String : 5
Updated String : 0005
In [9]: numStr = "5"
...: print('Original String :', numStr)
...:
...: # Make string right justified of length 4 by padding 3 spaces to left
...: numStr = numStr.rjust(4, ' ')
...:
...: print('Updated String :', numStr)
Original String : 5
Updated String : 5
In [10]: numStr = "5"
...: print('Original String :', numStr)
...:
...: # Make string right justified of length 4 by padding 3 '-' to left
...: numStr = numStr.rjust(4, '-')
...:
...: print('Updated String :', numStr)
Original String : 5
Updated String : ---5
In [11]: numStr = "45"
...: print('Original String :', numStr)
...:
...: # Make string left justified of length 5 by padding 3 0s to the right of it
...: numStr = numStr.ljust(5, '0')
...:
...: print('Updated String :', numStr)
Original String : 45
Updated String : 45000
In [12]: userName = "John"
...: print('Original String :', userName)
...:
...: # Make string left justified of length 7 by padding 3 spaces to the right of it
...: userName = userName.ljust(7, ' ')
...:
...: print('Updated String :' , userName, 'is')
Original String : John
Updated String : John is
In [13]: userName = "John"
...: print('Original String :', userName)
...:
...: # Make string left justified of length 7 by padding 3 '-' to the right of it
...: userName = userName.ljust(7, '-')
...:
...: print('Updated String :' , userName)
Original String : John
Updated String : John---
In [14]: firstStr = "sample"
...: secStr = "sample"
In [15]: if firstStr == secStr:
...: print('Both Strings are same')
...: else:
...: print('Strings are not same')
...:
Both Strings are same
In [16]: firstStr = "SAMple"
...: secStr = "sample"
In [17]: if firstStr.lower() == secStr.lower():
...: print('Both Strings are same')
...: else:
...: print('Strings are not same')
...:
Both Strings are same
In [18]: firstStr = "this is"
...: secStr = "not this"
In [19]: if firstStr != secStr:
...: print('Both Strings are not equal')
...: else:
...: print('Strings are equal')
...:
Both Strings are not equal
In [20]: if "abcd" > "abcc":
...: print('"abcd" is greater than "abcc"')
...:
...: if "Abc" < "abc":
...: print('"Abc" is less than "abc"')
...:
...: if "abcdd" > "abc":
...: print('"abcdd" is greater than "abc"')
...:
"abcd" is greater than "abcc"
"Abc" is less than "abc"
"abcdd" is greater than "abc"
is 比较字符串是否指向相同的内容:内容相同,内存地址相同
In [25]: firstStr = "sample"
...: secStr = "sample"
In [26]: if firstStr is secStr:
...: print('Both the objects are equal i.e. points to same object')
...:
...: print("Object ID of First object :" , id(firstStr))
...: print("Object ID of Second object :", id(secStr))
Both the objects are equal i.e. points to same object
Object ID of First object : 2601013061256
Object ID of Second object : 2601013061256
In [27]: if firstStr is secStr:
...: print('Both the objects are equal')
...:
Both the objects are equal
In [29]: secStr = "sample is".split()[0]
In [30]: secStr
Out[30]: 'sample'
In [31]: print('firstStr: ', firstStr, " : Object ID :", id(firstStr))
...: print('secStr: ', secStr, " : Object ID :", id(secStr))
firstStr: sample : Object ID : 2601013061256
secStr: sample : Object ID : 2601044964272
In [32]: if firstStr is secStr:
...: print('Both the objects are equal i.e. points to same object')
...: else:
...: print('Both the objects are different')
...:
Both the objects are different
== 运算符比较字符串内容是否相同:仅表征内容相同
In [33]: if firstStr == secStr:
...: print('Contents of both Strings are same')
...:
Contents of both Strings are same
In [34]: import re
...:
...: # Create regex pattern
...: regexPattern = re.compile("192.122.78.*")
...:
...: listOfIps = [ "192.122.78.11", "192.122.78.305" , "192.122.77.111"]
...:
...: # Check if strings in list matches the regex pattern
...: for ipStr in listOfIps:
...: matchObj = regexPattern.fullmatch(ipStr)
...: if matchObj:
...: print('Contents of string ' ,ipStr , ' matched the pattern')
...: else:
...: print('Contents of string ' ,ipStr , ' do not matched the pattern')
...:
Contents of string 192.122.78.11 matched the pattern
Contents of string 192.122.78.305 matched the pattern
Contents of string 192.122.77.111 do not matched the pattern