是否可以根据用户输入Python添加到字符串中
我目前正在处理一个吸收用户输入的程序,并且根据用户输入字符串应更改。我想知道,一旦收到用户输入,我是否有一种方法可以更改字符串。以下是示例代码。
title = input('Title: ')
subtitle = input('Subtitle: ')
chapter = input('Chapter: ')
subchapter = input('Subchapter: ')
title1 = '/title{}'.format(title)
subtitle1 = '/subtitle{}'.format(subtitle)
chapter1 = '/chapter{}'.format(chapter)
subchapter1 = '/subchapter{}'.format(subchapter)
output_txt = title1+subtitle1+chapter1+subchapter1
print(output_txt)
- 用户获取的输入:应该是一个数字
- ,然后将输入格式化为
- 基于用户输入字符串
的透视字符串。方案1: 用户输入
Title: 4
Subtitle:
Chapter: 12
Subchapter: 1
output_txt应该是
output_txt = '/title4/chapter12/subchapter1'
方案2: 用户输入
Title: 9
Subtitle:
Chapter: 2
Subchapter:
output_txt应该是
output_txt = '/title9/chapter2'
我一直在使用elif,但是由于可能有多种组合,所以我认为这样做不是最有效的。
朝着正确方向的任何帮助或技巧都非常感谢
I am currently working on a program that takes in user inputs, and depending on that users input the string should change. I was wondering if there was a way I could alter the string once the user input has been received. The following is a sample code.
title = input('Title: ')
subtitle = input('Subtitle: ')
chapter = input('Chapter: ')
subchapter = input('Subchapter: ')
title1 = '/title{}'.format(title)
subtitle1 = '/subtitle{}'.format(subtitle)
chapter1 = '/chapter{}'.format(chapter)
subchapter1 = '/subchapter{}'.format(subchapter)
output_txt = title1+subtitle1+chapter1+subchapter1
print(output_txt)
- Input taken by user: Should be a number
- The input would then be formatted to its perspective string
- Based on the user input the string, output_txt, should be formatted accordingly
Scenario 1:
User Input
Title: 4
Subtitle:
Chapter: 12
Subchapter: 1
output_txt should be
output_txt = '/title4/chapter12/subchapter1'
Scenario 2:
User Input
Title: 9
Subtitle:
Chapter: 2
Subchapter:
output_txt should be
output_txt = '/title9/chapter2'
I have been using if elif but since there could be multiple combinations I do not think doing it that way is the most efficient.
Any help or tips in the right direction is greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在为变量分配字符串值时,您可以使用IF-ELSE条件
You could use an if-else condition while assigning string values to the variable
让我向您介绍 typer
typer将帮助您用Python轻松地创建一个CLI,
以便您的代码 轻松创建CLI接近这样的接近
,您只需要通过为每个所需的参数添加参数来运行它
Let me introduce you to typer
typer will help you to create a CLI with python easily
for your code can be approached like this
And you just need to run it by adding the parameter for each one you need
这是一种使用正则表达式进行输入验证的方法,并列出综合以收集输入并构建输出字符串。
get_input()函数期望用户输入的每个值是一个正整数。所有其他输入都被默默地忽略(无将返回)。
Here's an approach that uses a regular expression for input validation and list comprehensions to gather the inputs and build the output string.
The get_input() function expects each value entered by the user to be a positive integer. All other input is silently ignored (and
None
is returned).只是为了完整性,您可能还需要测试一个数字。
Just for completeness you might want to test for a number as well.
您可以做这样的事情...
输出...
显然,您需要为您的特定目的为代码添加一些更改。
You can do something like this...
Output...
Obviously you need to add few changes to the code for your specific purpose.