Files
Python_CookBook_repo/2.字符串和文本/14.字符串连接与合并.py
2025-09-10 16:12:45 +08:00

16 lines
592 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

if __name__ == "__main__":
# 如果有一堆字符串, 那我想join函数是一个好选择, 但如果就几个那用+就可以了
parts = ["Is", "Chicago", "Not", "Chicago?"]
str_c = ' '.join(parts)
print(str_c)
# 如果我有密钥的两部分想把a和b连接在一起那可以这样
keyword = 'su' 'ka'
print(keyword)
# 在实际运用中,如果遇到一堆字符串拼接,千万别用+=,这样会产生大量内存垃圾
# 如果有大量的片段需要拼接,则应该考虑使用生成器函数(字段确定的情况下)