python - Python:附加到一个空字符串
问题描述
我目前正在做一个项目,我想添加字符/gr
barfoo = ""
# Something that adds 'hel' to barfoo?
# Something that adds 'lo' to barfoo?
print(barfoo)
> 'hello'
我怎么会做这样的事情?请注意,我知道将它添加到列表中并简单地“压缩”它,但我想知道是否有更简单的方法。
解决方案
要么以空字符串开头并连接,要么以空列表开头并加入。
barfoo = ''
barfoo += 'h'
barfoo += 'i'
print(barfoo)
...
barfoo = []
barfoo.append('h')
barfoo.append('i')
print(''.join(barfoo))
推荐阅读
- c# - 改进 LINQ to EF 生成的 SQL 查询
- android - 如何使用地理围栏创建一个 Android 应用程序,当用户到达目的地时,它会自动获得奖励而不做任何事情?
- jaxb - 为 xjc 优化外部绑定 XML
- asp.net - Dotnet 新 - 缺少 webpack.js
- spring - JPQL - 连接 - MultipleTables 和空 ResultList
- mysql - 比较同一列MySQL的计数以获得百分比
- linux - 在 bash 脚本中安全地记住 ssh 凭据
- r - knn 错误:不允许缺失值 - 我的数据中没有任何缺失值
- sql - 应用程序角色和扩展程序权限
- python - 无法从电子邮件原始数据中解码 base64 字符串