Python字符串基础与常用方法实战指南

竹里
竹里
发布时间:2026-10-13| 2人浏览过
原创

本文深入解析Python中字符串的定义方式与核心操作方法。内容涵盖字符串变量的类型标识、单双引号的使用规范,以及字符串连接、重复和长度计算等基础操作。重点介绍了count、find、islower等常用字符串方法及其参数差异,并通过代码示例演示了如何处理不同数据类型的方法调用限制,帮助开发者掌握Python字符串处理技巧。

Python字符串基础与常用方法实战指南

本文通过具体实例详细讲解Python中字符串及其相关方法的用法,供读者参考学习。

字符串定义与类型标识

字符串概述

在Python中,字符串变量的类型标识为 str。定义字符串时,既可以使用双引号 ",也可以使用单引号 '。

字符串定义示例


my_string = 'this is a string!'
my_string2 = "this is also a string!!!"
# Also , we can use backslash '/' to escape quotes.
this_string = 'Simon\'s skateboard is in the garage.'
print(this_string)

字符串基础操作与练习

字符串的常见操作方式


first_word = 'Hello'
second_word = 'There'
print(first_word + second_word) # HelloThere
print(first_word + ' ' + second_word) # Hello There
print(first_word * 5) # HelloHelloHelloHelloHello
print(len(first_word)) # 5
print(first_word[0]) # H
print(first_word[1]) # e

字符串相关练习

在字符串中正确嵌入引号


ford_quote = 'Whether you think you can, or you think you can\'t--you\'re right.'
print(ford_quote) # Whether you think you can, or you think you can't--you're right.

以下代码片段的执行结果是什么?


coconut_count = "34"
mango_count = "15"
tropical_fruit_count = coconut_count + mango_count
print(tropical_fruit_count) # 3415 (并且 tropical_fruit_count 是字符串)

构建服务器日志消息


username = "Kinari"
timestamp = "04:50"
url = "http://petshop.com/pets/mammals/cats"
# TODO: print a log message using the variables above. The message should have the same format as this one: "Yogesh accessed the site http://petshop.com/pets/reptiles/pythons at 16:20."
print(username + ' accessed the site ' + url + ' at ' + timestamp + '.')

利用字符串拼接技术与 len 函数,计算电影明星全名的字符长度。


given_name = "William"
middle_names = "Bradley"
family_name = "Pitt"
name_length = len(given_name + ' ' + middle_names + ' ' + family_name)
# Now we check to make sure that the name fits within the driving license character limit
driving_license_character_limit = 28
print(name_length <= driving_license_character_limit) # True

前文已使用 len 函数获取字符串长度。若向 len 函数传入整数835而非字符串,该函数将返回什么结果?

报错(Error)

字符串方法详解与类型限制

字符串方法详解

Python中的方法与函数类似,但方法专门针对已创建的变量进行操作。每个方法都严格对应其所在变量的特定数据类型。

注:图片来源网络

所有方法均将字符串本身作为第一个参数传入,但部分方法还接受额外参数。以下通过几个示例展示具体输出结果。


my_string = "sebastian thrun"
my_string.islower() # True
my_string.count('a') # 2
my_string.find('a') # 3

从示例可见,count 和 find 方法均支持接收额外参数,而 islower 方法不接受任何额外参数。若变量存储的是浮点数、整数或其他数据类型,其可用的方法集合将完全不同。

字符串方法相关练习

  • 若对浮点数对象调用 islower 等方法,例如执行 13.37.islower(),会发生什么情况?
  • 系统将抛出错误。islower 属于字符串专属方法,并非浮点数方法。不同数据类型拥有各自特定的方法集,例如浮点数具备 is_integer 方法,而字符串不具备该方法。
  • 练习字符串方法

my_name = "my name is Joh."
cap = my_name.capitalize()
print(cap) # My name is joh.
ew = my_name.endswith('li')
print(ew) # False
ind = my_name.index('is')
print(ind) # 8

如需了解更多Python相关知识,建议查阅本站专题:《Python字符串操作技巧汇总》、《Python数据结构与算法教程》、《Python列表(list)操作技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》及《Python入门与进阶经典教程》。

希望本文所述内容能对您的Python程序设计学习有所帮助。

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。