python字符串的最大长度

简介:

今天在写脚本过程中需要将大量字符存入python字符串,遂好奇python的字符串是否有长度限制。

经查询文档未发现提及长度限制,后来在stackoverflow找到了相关讨论:
_
原文链接:link
大意为无环境限制下python的string中存储极限字符数量是多少,其中一位网友进行了实验,以下为相关代码:

def create1k():
    s = ""
    for i in range(1024):
        s += '*'
    return s

def create1m():
    s = ""
    x = create1k()
    for i in range(1024):
        s += x
    return s

def create1g():
    s = ""
    x = create1m()
    for i in range(1024):
        s += x
    return s

print("begin")
s = ""
x = create1g()
for i in range(1024):
    s += x
    print(str(i) + "g ok")
    print(str(len(s)) + ' bytes')

测试结果:在使用了116G内存后报内存错误。
_

结论:python本身对string长度无强制性限制。使用过程中主要需要考虑电脑性能和程序效率。

目录
相关文章
|
16天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
203 100
|
16天前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
243 99
|
20天前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
20天前
|
开发者 Python
Python f-strings:更优雅的字符串格式化技巧
Python f-strings:更优雅的字符串格式化技巧
|
20天前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
1月前
|
Python
使用Python f-strings实现更优雅的字符串格式化
使用Python f-strings实现更优雅的字符串格式化
|
2月前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
218 92
|
2月前
|
索引 Python
python 字符串的所有基础知识
python 字符串的所有基础知识
190 0
|
2月前
|
Python
Python字符串center()方法详解 - 实现字符串居中对齐的完整指南
Python的`center()`方法用于将字符串居中,并通过指定宽度和填充字符美化输出格式,常用于文本对齐、标题及表格设计。

热门文章

最新文章

推荐镜像

更多