ITPub博客

首页 > 数据库 > 数据库开发技术 > Python字符串处理常用的30种操作

Python字符串处理常用的30种操作

原创 数据库开发技术 作者:derekwh 时间:2024-06-20 00:01:00 0 删除 编辑

我们平时编写代码时,经常需要对字符串进行处理,本文详细介绍Python处理字符串常用的30种操作,并给出了对应的代码。

  1. 分割

使用split()方法将字符串按照指定的分隔符进行分割。

s = "Hello,World"
result = s.split(",")
print(result)  # 输出:['Hello', 'World']
  1. 替换

使用replace()方法将字符串中的指定子串替换为新的子串。

s = "Hello,World"
result = s.replace("World", "Python")
print(result)  # 输出:Hello,Python
  1. 查找

使用find()方法查找子串在字符串中的位置。

s = "Hello,World"
result = s.find("World")
print(result)  # 输出:6
  1. 计数

使用count()方法统计子串在字符串中出现的次数。

s = "Hello,World,World"
result = s.count("World")
print(result)  # 输出:2
  1. 大小写转换

使用upper()和lower()方法将字符串转换为大写或小写。

s = "Hello,World"
result_upper = s.upper()
result_lower = s.lower()
print(result_upper)  # 输出:HELLO,WORLD
print(result_lower)  # 输出:hello,world
  1. 首字母大写

使用capitalize()方法将字符串的首字母转换为大写。

s = "hello,world"
result = s.capitalize()
print(result)  # 输出:Hello,world
  1. 字符串居中对齐

使用center()方法将字符串居中对齐,并使用指定的字符填充两侧。

s = "Hello,World"
result = s.center(15, "-")
print(result)  # 输出:--Hello,World--
  1. 字符串左对齐

使用ljust()方法将字符串左对齐,并使用指定的字符填充右侧。

s = "Hello,World"
result = s.ljust(20, "-")
print(result)  # 输出:Hello,World--------
  1. 字符串右对齐

使用rjust()方法将字符串右对齐,并使用指定的字符填充左侧。

s = "Hello,World"
result = s.rjust(20, "-")
print(result)  # 输出:--------Hello,World
  1. 去除空格

使用strip()方法去除字符串两端的空格。

s = " Hello,World "
result = s.strip()
print(result)  # 输出:Hello,World
  1. 判断是否以指定子串开头

使用startswith()方法判断字符串是否以指定子串开头。

s = "Hello,World"
result = s.startswith("Hello")
print(result)  # 输出:True
  1. 判断是否以指定子串结尾

使用endswith()方法判断字符串是否以指定子串结尾。

s = "Hello,World"
result = s.endswith("World")
print(result)  # 输出:True
  1. 判断是否全为数字

使用isdigit()方法判断字符串是否全为数字。

s = "123"
result = s.isdigit()
print(result)  # 输出:True
  1. 判断是否全为字母

使用isalpha()方法判断字符串是否全为字母。

s = "abc"
result = s.isalpha()
print(result)  # 输出:True
  1. 判断是否全为空白字符

使用isspace()方法判断字符串是否全为空白字符。

s = " "
result = s.isspace()
print(result)  # 输出:True
  1. 判断是否为标题格式

使用istitle()方法判断字符串是否为标题格式(每个单词首字母大写)。

s = "Hello World"
result = s.istitle()
print(result)  # 输出:True
  1. 判断是否为小写字母

使用islower()方法判断字符串是否全为小写字母。

s = "hello"
result = s.islower()
print(result)  # 输出:True
  1. 判断是否为大写字母

使用isupper()方法判断字符串是否全为大写字母。

s = "HELLO"
result = s.isupper()
print(result)  # 输出:True
  1. 判断是否为标识符

使用isidentifier()方法判断字符串是否为合法的标识符(变量名)。

s = "hello"
result = s.isidentifier()
print(result)  # 输出:True
  1. 判断是否为数字类型

使用isnumeric()方法判断字符串是否为数字类型(整数或浮点数)。

s = "123"
result = s.isnumeric()
print(result)  # 输出:True
  1. 拼接
str1 = "Hello"
str2 = "World"
result = str1 + str2
print(result)  # 输出:HelloWorld
  1. 字符串复制
str1 = "Hello"
result = str1 * 3
print(result)  # 输出:HelloHelloHello
  1. 字符串长度
str1 = "Hello"
length = len(str1)
print(length)  # 输出:5
  1. 字符串格式化
name = "Tom"
age = 18
result = "My name is {} and I am {} years old.".format(name, age)
print(result)  # 输出:My name is Tom and I am 18 years old.
  1. 字符串切片
str1 = "Hello,World"
result = str1[0:5]
print(result)  # 输出:Hello
  1. 字符串连接
str1 = "Hello"
str2 = "World"
result = "-".join([str1, str2])
print(result)  # 输出:Hello-World
  1. 字符串转数字
str1 = "123"
num = int(str1)
print(num)  # 输出:123
  1. 获取字符串中的数字
import re

def get_numbers_from_string(s):
    return re.findall(r'\d+', s)

s = "abc123def456"
numbers = get_numbers_from_string(s)
print(numbers)  # 输出:['123', '456']
  1. 获取字符串中的字母
import re

def get_letters_from_string(s):
    return re.findall(r'[a-zA-Z]+', s)

s = "abc123def456"
letters = get_letters_from_string(s)
print(letters)  # 输出:['abc', 'def']

30.反转字符串

s = "Hello, World!"
reversed_s = s[::-1]
print(reversed_s)  # 输出:!dlroW ,olleH

本文介绍了多种字符串处理的方法,包括字符串的拼接、分割、替换等操作。这些方法可以帮助大家更高效地处理和分析文本数据。在实际编程中,大家可根据具体需求选择合适的方法来实现字符串的处理和分析。

原文作者:Python学习杂记;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论
央企算法工程师,微信公众号: python数智工坊

注册时间:2021-03-14

  • 博文量
    305
  • 访问量
    162396