首页 > 解决方案 > 打印组合成连续字符串的输入字符串

问题描述

有人可以帮我为 cli 编写一个参数,以便它可以将“输入字符串”打印为组合的连续字符串吗?我如何使它成为真正的代码来做我想做的事?

parser.add_argument('-c','--combine', action='store', dest='store_combined', help='Print input strings combined in a continuous string')

args = parser.parse_args()

所以如果我运行 $ python HW3_cli.py -cThese Strings Get Concatenated 然后我得到TheseStringsGetConcatenated一个打印。

我还可以打印每个字符串的长度吗?

标签: pythonarguments

解决方案


您可以尝试将字符串放在引号中:

python HW3_cli.py -c "These Strings Get Concatenated" 

然后在您的代码中,您可以像这样按空格分隔:

args = parser.parse_args() 
print(args.store_combined.split())
#['These', 'Strings', 'Get', 'Concatenated']

推荐阅读