python --创建固定字符串长度,先进先出

python 复制代码
a = '123'

def concatenate_within_limit(b, new_string):
    # 计算新字符串与a的长度之和
    a = b
    total_length = len(a) + len(new_string)

    # 如果长度超过1024,从前面删除足够的字符
    if total_length > 5:
        diff = total_length - 5
        a = a[diff:] + new_string  # 删除前diff个字符,并拼接新字符串
    else:
        a += new_string  # 长度未超过1024,直接拼接新字符串

    # 返回处理后的a
    return a

for i in range(4,10):
    print(concatenate_within_limit(a, str(i)))
相关推荐
没有梦想的咸鱼185-1037-16637 分钟前
AI-Python机器学习、深度学习核心技术与前沿应用及OpenClaw、Hermes自动化编程
人工智能·python·深度学习·机器学习·chatgpt·数据挖掘·数据分析
雪度娃娃30 分钟前
转向现代C++——优先选用nullptr而不是0和NULL
开发语言·c++
axinawang33 分钟前
第3课:变量与输入
python
idingzhi1 小时前
A股量化策略日报()
python
zyk_computer1 小时前
AI 时代,或许 Rust 比 Python 更合适
人工智能·后端·python·ai·rust·ai编程·vibe coding
weixin199701080161 小时前
【保姆级教程】淘宝/天猫商品详情 API(item_get)接入指南:Python/Java/PHP 调用示例与 JSON 返回值解析
java·python·php
萌新小码农‍1 小时前
python装饰器
开发语言·前端·python
KK溜了溜了1 小时前
Python从入门到精通
服务器·开发语言·python
2401_884454151 小时前
mysql处理复杂SQL性能_InnoDB优化器与MyISAM差异
jvm·数据库·python
故事和你911 小时前
洛谷-【图论2-1】树5
开发语言·数据结构·c++·算法·动态规划·图论