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长度无强制性限制。使用过程中主要需要考虑电脑性能和程序效率。

目录
相关文章
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
688 100
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
788 99
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
开发者 Python
Python f-strings:更优雅的字符串格式化技巧
Python f-strings:更优雅的字符串格式化技巧
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
使用Python f-strings实现更优雅的字符串格式化
使用Python f-strings实现更优雅的字符串格式化
|
索引 Python
python 字符串的所有基础知识
python 字符串的所有基础知识
621 0
Python字符串center()方法详解 - 实现字符串居中对齐的完整指南
Python的`center()`方法用于将字符串居中,并通过指定宽度和填充字符美化输出格式,常用于文本对齐、标题及表格设计。
|
PHP Python
Python format()函数高级字符串格式化详解
在 Python 中,字符串格式化是一个重要的主题,format() 函数作为一种灵活且强大的字符串格式化方法,被广泛应用。format() 函数不仅能实现基本的插入变量,还支持更多高级的格式化功能,包括数字格式、对齐、填充、日期时间格式、嵌套字段等。 今天我们将深入解析 format() 函数的高级用法,帮助你在实际编程中更高效地处理字符串格式化。
903 0

推荐镜像

更多