前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python基础篇 strings 02

Python基础篇 strings 02

作者头像
披头
发布2019-12-26 11:00:36
3280
发布2019-12-26 11:00:36
举报
文章被收录于专栏:datartisandatartisan

Python基础,strings 02

移除字符字符填充字符串比较

移除字符

stringObject[ start : stop : interval]

  • 移除指定索引字符
代码语言:javascript
复制
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
  • 移除首字符
代码语言:javascript
复制
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
  • 移除尾字符
代码语言:javascript
复制
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
  • 移除多个字符
代码语言:javascript
复制
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
字符填充
  • 使用 string.zfill() 左填充 0
代码语言:javascript
复制
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
  • 使用 string.rjust() 左填充 空格
代码语言:javascript
复制
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
  • 使用 string.rjust() 左填充 其他字符
代码语言:javascript
复制
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
  • 使用 string.ljust() 右填充 0
代码语言:javascript
复制
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
  • 使用 string.ljust() 右填充 空格
代码语言:javascript
复制
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
  • 使用 string.ljust() 右填充 其他字符
代码语言:javascript
复制
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---
字符串比较
  • 使用 == 操作符比较字符串是否相同
代码语言:javascript
复制
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
  • 忽略大小写,比较字符串
代码语言:javascript
复制
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
  • 使用 != 操作符比较字符串是否不同
代码语言:javascript
复制
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
  • 比较字符串大小
代码语言:javascript
复制
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 vs ==

is 比较字符串是否指向相同的内容:内容相同,内存地址相同

代码语言:javascript
复制
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

代码语言:javascript
复制
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

== 运算符比较字符串内容是否相同:仅表征内容相同

代码语言:javascript
复制
In [33]: if firstStr == secStr:
    ...:     print('Contents of both Strings are same')
    ...:
Contents of both Strings are same
  • 使用正则表达式比较字符串
代码语言:javascript
复制
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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 乐享数据8090 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 移除字符
  • 字符填充
  • 字符串比较
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档