简体   繁体   English

如何将字符串插入引号,同时将字符串输入保留在 Python 中?

[英]How can I insert a string into quotations while keeping the string input in Python?

Random = (random.choice(Usernames))
print (str (Random))
studio.invite_curator (Random)

Text input in quotations is needed to complete the last line, yet I have a string that I need in the quotations.需要在引号中输入文本来完成最后一行,但我在引号中有一个字符串需要。 Is there any way to keep the quotations and the string in a new string variable?有没有办法将引号和字符串保存在新的字符串变量中?

This is just a piece of code from my project, but I know there are not any problems with the other parts I do not have shown.这只是我项目中的一段代码,但我知道我没有展示的其他部分没有任何问题。

I tried to use this script:我试图使用这个脚本:

Random2 = '"',Random,'"'
studio.invite_curator (Random2)

But outputted as an error, because the final line of what I showed above needs to be in quotations.但是输出为错误,因为我上面显示的最后一行需要用引号引起来。 I can not yet find a way to work around this.我还找不到解决这个问题的方法。

Use string concatenation instead of commas.使用字符串连接而不是逗号。

Random2 = '"' + Random + '"'

This will create a new string variable Random2 which contains the value of Random enclosed in quotation marks.这将创建一个新的字符串变量 Random2,其中包含用引号引起来的 Random 值。 Alternatively you could use f-strings to insert the variable into the string.或者,您可以使用 f-strings 将变量插入到字符串中。

Random2 = f'"{Random}"'
studio.invite_curator(Random2)

This will also create a new string variable Random2 which contains the value of Random enclosed in quotation marks.这还将创建一个新的字符串变量 Random2,其中包含用引号引起来的 Random 值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有引号的情况下分隔 raw_input 字符串? 只有逗号和空格 - How can I separate the raw_input string without quotations mark? only comma and spaces only 如何有效地替换字符串中的双引号和单引号? - How can I replace double and single quotations in a string efficiently? 带有引号的Python 2.7.6字符串文字错误 - Python 2.7.6 String Literal error with Quotations in Quotations Python 2.7 获取用户输入并作为不带引号的字符串进行操作 - Python 2.7 getting user input and manipulating as string without quotations 正则表达式删除多余的引号,以便我可以将字符串加载为 JSON -Python - Regexp to remove extra quotations marks so I can load the string as JSON -Python 如何在Python中的SQL查询的值列表中删除字符串的引号 - How to remove quotations of string in list of values for sql query in python 如何从 Python itertools 输出中删除字符串引号、逗号和括号? - How to remove string quotations, commas and parenthesis from Python itertools output? 如何解析Python代码同时保持字符串文字完全一样? - How to parse Python code while keeping string literals exactly as-is? 如何计算嵌套列表的平均值,同时仍将其与字符串关联 - How can I calculate an average of a nested list while still keeping it associated with a string 如何将缩写序数的大小写更低,同时将标题的其余部分保留在标题大小写中? - How can I change the case of abbreviated ordinals to lower while keeping the rest of the string in title case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM