在python中的字符串中插入字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36050848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
inserting characters in strings in python
提问by David
I'm doing homework and I have all except this last problem figured out. I can't seem to figure out how to get a character not to show up if it's inserted at a larger insertion point than the string has. This is the problem I'm working on:
我正在做作业,除了最后一个问题之外,我已经解决了所有问题。如果将字符插入到比字符串更大的插入点,我似乎无法弄清楚如何让字符不显示。这是我正在解决的问题:
Write a program insert.py that simulates insert behavior on a string.
The program takes three inputs: a character to be inserted, its position, and a string into which a character is to be inserted. The program prints the new version of the string. For example, if the arguments are: -, 2, below, then the program prints be-low. If a position passed to the program is outside of the bounds of the original string (in this case <0 or >5 -nothing would append on the end of below), then print the original string, without any changes (Note: an insert value of 0 OR the length of the string will add to the start or append to the end, i.e. T, 3, can will be canT; T, 0, can will be Tcan, and T, 4, can will be can).
编写一个程序 insert.py 来模拟对字符串的插入行为。
该程序接受三个输入:要插入的字符、它的位置和要插入字符的字符串。该程序打印字符串的新版本。例如,如果参数是:-, 2, below,则程序打印 be-low。如果传递给程序的位置在原始字符串的边界之外(在这种情况下 <0 或 >5 - 在下面的末尾不会追加任何内容),则打印原始字符串,不做任何更改(注意:插入值 0 或字符串的长度将添加到开头或附加到结尾,即 T, 3, can 将是 canT;T, 0, can 将是 Tcan,而 T, 4, can 将是 can)。
This is what I have done so far:
这是我到目前为止所做的:
C = input("Choose your charecter to insert. ")
P = int(input("Choose your character's position. "))
S = input("Choose your string. ")
st = S[:P] + C + S[P:]
print(st)
print(C, P, S)
采纳答案by idjaw
Just keep it simple. Check to see if the position is greater than the length of the word then just print the word, else proceed with your logic:
保持简单。检查位置是否大于单词的长度然后只打印单词,否则继续你的逻辑:
C = input("Choose your charecter to insert. ")
P = int(input("Choose your character's position. "))
S = input("Choose your string. ")
if P > len(S):
print(S)
else:
st = S[:P] + C + S[P:]
print(st)
print(C, P, S)
回答by Nick Pandolfi
Theres also this :)
还有这个:)
result = list(S).insert(P, C)
if result:
print(result)
else:
print(S)
回答by Vorashil Farzaliyev
I hope you have found the answer you are looking for already. But here I would like to suggest my version.
我希望你已经找到了你正在寻找的答案。但在这里我想推荐我的版本。
C = input("Choose your charecter to insert. ")
P = int(input("Choose your character's position. "))
S = input("Choose your string. ")
print(S if P>len(S) else S[:P] + C + S[P:])
回答by Satvinder Kaur
We converted the string into a list and then change the value(update the string with inserting a character between a string).
我们将字符串转换为列表,然后更改值(通过在字符串之间插入一个字符来更新字符串)。
def anystring(string, position, character):
list2=list(string)
list2[position]=character
return ''.join(list2)`