网站首页 > 技术文章 正文
@Author :Runsen

Python字符串总结
什么字符串
字符串是由独立字符组成的一个序列,通常包含在单引号(‘ ’),双引号(”“)
三引号(''' ''')
s1 = 'hello'
s2 = "hello"
s3 = """hello"""
s1 == s2 == s3
True
三引号字符串常用于函数的注释
def calculate_similarity(item1, item2):
"""
Calculate similarity between two items
Args:
item1: 1st item
item2: 2nd item
Returns:
similarity score between item1 and item2
"""
转义字符
用 \ 开头的字符串,来表示一些特定意义的字符
s = 'a\nb\tc'
print(s)
a
b c
len(s)
5
代码中的'\n',表示一个字符——换行符;'\t'也表示一个字符,四个空格
字符 a,换行,字符 b,然后制表符,最后打印字符 c 最后打印的输出横跨了两行,但是整个字符串 s 仍然只有 5
常用操作
name = 'jason'
name[0]
'j'
name[1:3]
'as'
for char in name:
print(char)
j
a
s
o
n
注意python的字符串是不可变的
s = 'hello'
s[0] = 'H'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
只能通过船创建新的字符串
s = 'H' + s[1:]
s = s.replace('h', 'H')
在java 中有可变的字符串,StringBuilder ,每次改变字符串,无需创建新的字符串,时间复杂度为O(1)
但是在python中如果想要改变字符串,往往需要O(n)的时间复杂度,n是新字符串的长度
拼接字符串
str1 += str2 # 表示 str1 = str1 + str2
# 这个时间复杂度是多少
s = ''
for n in range(0, 100000):
s += str(n)
在python2中总的时间复杂度就为 O(1) + O(2) + … + O(n) = O(n^2)
但是在python3中 str1 += str2 首先会检测str1 是否有其他的引用
所以在python3中时间复杂度是O(n)
l = []
for n in range(0, 100000):
l.append(str(n))
l = ' '.join(l)
由于列表的 append 操作是 O(1) 复杂度,时间复杂度为 n*O(1)=O(n)。
split分割
def query_data(namespace, table):
"""
given namespace and table, query database to get corresponding
data
"""
path = 'hive://ads/training_table'
namespace = path.split('//')[1].split('/')[0] # 返回'ads'
table = path.split('//')[1].split('/')[1] # 返回 'training_table'
data = query_data(namespace, table)
- string.strip(str),表示去掉首尾的 str
- tring.lstrip(str),表示只去掉开头的 str
- string.rstrip(str),表示只去掉尾部的 str
在读入文件时候,如果开头和结尾都含有空字符,就采用strip函数
s = ' my name is jason '
s.strip()
'my name is jason'
格式化
format
print('no data available for person with id: {}, name: {}'.format(id, name))
%
print('no data available for person with id: %s, name: %s' % (id, name))
%s 表示字符串型,%d 表示整型
两种字符串拼接操作,哪个更好
s = ''
for n in range(0, 100000):
s += str(n)
l = []
for n in range(0, 100000):
l.append(str(n))
s = ' '.join(l)
# 第一个 +=
import time
start_time =time.perf_counter()
s = ''
for n in range(0,1000000):
s += str(n)
end_time = time.perf_counter()
# 5.7604558070000005
print(end_time - start_time)
# 第二个 join
import time
start_time =time.perf_counter()
s = []
for n in range(0,1000000):
s.append(str(n))
''.join(s)
end_time = time.perf_counter()
# 0.622547053
print(end_time - start_time)
# 第三个 map
import time
start_time = time.perf_counter()
s = ''.join(map(str, range(0, 1000000)))
end_time = time.perf_counter()
# 0.403433529
print(end_time - start_time)
对于数据量大的map好过join,join好过 +=
对于数据量小的map 好过 += 好过join

猜你喜欢
- 2024-12-25 Python 标准库中鲜为人知的宝藏 | Node.js 22.8.0 发布
- 2024-12-25 Python连接数据库的三种方式
- 2024-12-25 Python入门学习记录之一:变量
- 2024-12-25 Python之Json模块详解
- 2024-12-25 告别重复工作,Python操作xlwings实例演示
- 2024-12-25 (新版)Python 分布式爬虫与 JS 逆向进阶实战吾爱分享
- 2024-12-25 (新版)Python 分布式爬虫与 JS 逆向进阶实战无mi-朝朝暮暮
- 2024-12-25 python 爬虫调用 js 的库之 execjs
- 2024-12-25 代码示例:Python 调用并执行 JS
- 2024-12-25 Python、JavaScript和Rust的Web性能比较
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)