python 学习笔记(二)之 字符串操作

1、比较

(1) 相等不等比较

temp1="1"
temp2="2"
if temp1 != temp2:
   print("temp1 != temp2")
else:
   print("temp1 == temp2")
temp1="1"
temp2="2"
if temp1 == temp2:
   print("temp1 == temp2")
else:
   print("temp1 != temp2")

(2) 大小比较

temp1="1.1.1"
temp2="1.2.1"

if temp1 > temp2:
   print("temp1 > temp2")
else:
   print("temp1 <= temp2")


输出结果为:
temp1 <= temp2

 通过查阅资料了解到 :String 通过 内置函数 ord() 获得每个字符的 Unicode 编码进行大小比较

2、匹配字符串

  有两种方式:

(1) if src in match:

     通过 if ... in .... 方式查看 src 中是否包含 match,当包含的时候 if 条件 为 true

(2) src.find(match)

     通过  find 方式 查看 src 中是否包含 match,返回首次匹配到的字符下标

(3) src.rfind(match)

     通过  find 方式 查看 src 中是否包含 match,返回最后一次匹配到的字符下标

def match(src_str,match_str):
    if match_str in src_str:
        print('use "if ... in ... " to match')
        print(f"src_str[{src_str}] 含有  match_str[{match_str}]")
    
    if match_str not in src_str:
        print('use "if ... in ... " to match')
        print(f"src_str[{src_str}] 不含有  match_str[{match_str}]")
    
    print('use "find" to match,返回首次匹配的下标,匹配不到 返回 -1')
    index = src_str.find(match_str)
    print(f"src_str[{src_str}],match_str[{match_str}],匹配到的下标为[{index}]")
    
    print('use "rfind" to match,返回最后一次匹配到的下标,匹配不到返回-1')
    index = src_str.rfind(match_str)
    print(f"src_str[{src_str}],match_str[{match_str}],匹配到的下标为[{index}]")


# 4. 匹配字符串
match("test111test222","test")
match("test111test222","sky")

 输出结果为:

use "if ... in ... " to match
src_str[test111test222] 含有  match_str[test]
use "find" to match,返回首次匹配的下标,匹配不到 返回 -1
src_str[test111test222],match_str[test],匹配到的下标为[0]
use "rfind" to match,返回最后一次匹配到的下标,匹配不到返回-1
src_str[test111test222],match_str[test],匹配到的下标为[7]
use "if ... in ... " to match
src_str[test111test222] 不含有  match_str[sky]
use "find" to match,返回首次匹配的下标,匹配不到 返回 -1
src_str[test111test222],match_str[sky],匹配到的下标为[-1]
use "rfind" to match,返回最后一次匹配到的下标,匹配不到返回-1
src_str[test111test222],match_str[sky],匹配到的下标为[-1]

 3、替换匹配的字符串

    str.replace(old,new[,max])  将 src 字符串中的 字符串 old 替换成  new ,若 max 没有传,表示将 str 所有 old 字符串替换成 new 字符串。若 max 设置值,表示 匹配上的从左到有替换 max 次。

  可以参考 菜鸟 学习网站。

def replaceTest(src_str,old,new,count=0):
    if count == 0:
        new_string = src_str.replace(old, new)
    else:
        new_string = src_str.replace(old, new, count)
    print(f"srcString is [{src_str}]old string is [{old}],new string is [{new}],count is [{count}],result is [{new_string}]")


# 3. 字符串替换
"""
  str.replace(old, new[, max])
  old -- 将被替换的子字符串。
  new -- 新字符串,用于替换old子字符串。
  max -- 可选字符串, 替换不超过 max 次
"""
replaceTest("test111test222","test","sky")
replaceTest("test111test222","test","sky",1)
replaceTest("test111test222","test","sky",2)
replaceTest("test111test222","111","sky",2)

 输出的结果为:

srcString is [test111test222]old string is [test],new string is [sky],count is [0],result is [sky111sky222]
srcString is [test111test222]old string is [test],new string is [sky],count is [1],result is [sky111test222]
srcString is [test111test222]old string is [test],new string is [sky],count is [2],result is [sky111sky222]
srcString is [test111test222]old string is [111],new string is [sky],count is [2],result is [testskytest222]

 4 、截取

 python 没有提供直接的截取字符串的方法,在Java中可以通过 substring 方法进行解决。在 python 中要达到该目的,必须使用 下标的方式。

# 5. 截取字符串
src_str = "test111test222"
start = src_str.find("test")
end = src_str.rfind("test")
substring_str = src_str[start:end]
print(f"src_str[{src_str}]截取第一次匹配到[test]和最后一次匹配到[test]之间的字符串[{substring_str}]")
print("----------------------------------")

 输出结果为:

src_str[test111test222]截取第一次匹配到[test]和最后一次匹配到[test]之间的字符串[test111]
----------------------------------

 显然想要达到该效果只能使用 str[start:end]的方式,其中:

  • start:开始截取的下标
  • end:结束的下标
  • 遵循左闭右开的原则

5、字符串判空

def empty(src_str):
    print('use "len(src_str) " to check') 
    if len(src_str) == 0:   
        print(f"src_str[{src_str}] is empty")
    else:
        print(f"src_str[{src_str}] cannot check")
    
    print('use "src.isspace() " to check')
    if src_str.isspace():
        print(f"src_str[{src_str}] is empty")
    else:
        print(f"src_str[{src_str}] cannot check")

输出结果:

字符串判空
use "len(src_str) " to check
src_str[] is empty
use "src.isspace() " to check
src_str[] cannot check
use "len(src_str) " to check
src_str[   ] cannot check
use "src.isspace() " to check
src_str[   ] is empty

从结果可以看出,如下结论:

  • 判断要用 len()方法,或者使用  src != ""
  • 判断字符串是否 null ,可以使用  if  src is not None:
原文地址:https://www.cnblogs.com/sandyflower/p/14152311.html