python - How to duplicate each character in a string
问题描述
How would I repeat a every character in a string twice?
Expected Output
input_string = "hello"
output_string = "hheelllloo"
My Code
def double_char(string):
for i in string:
# Place code here
def main():
user_string = input()
print(double_char(user_string))
main()
How can I complete the function double_char(string)?
解决方案
如果您想多次复制一个字符串,比如说 2,您可以这样做。
s = "a"
double_s = s*2
然后你可以在一个字符串中逐个字符地迭代字符:
for s in my_string:
# Here I do something with s, like for example duplicate it
因此,将这两种方法混合在一起会累积在字符串中重复的字符:
def double_char(string):
res = ""
for i in string:
res += i*2
return res
推荐阅读
- python - 将 PIL 图像下载到 CSV
- php - Wordpress Elementor 在通过 AJAX 加载帖子后不加载 CSS 样式
- sql - 使用 ilike 和 SQL 连接的 Ecto 查询的正确语法是什么?
- html - 当浏览器由 Selenium 启动时,即使手动也无法单击链接,但是当浏览器手动启动时它可以工作
- windows - 如何在 Windows 中的两个应用程序之间共享相同的端口?
- kubernetes - 排队后气流不会记录日志
- microsoft-graph-api - 枚举给定 SharePoint URL 并使用 Graph API 的特定驱动器的 DriveItem 资源
- laravel - 在 Nginx 上使用 laravel 自动索引
- postgresql - Django注解优化
- r - 你如何为ggplot中的条形图在x轴上做范围?