在 Python 中的给定索引处插入一些字符串到给定的字符串中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4022827/
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
Insert some string into given string at given index in Python
提问by james
I am newbie in Python facing a problem: How to insert some fields in already existing string?
我是 Python 新手,面临一个问题:如何在现有字符串中插入一些字段?
For example, suppose I have read one line from any file which contains:
例如,假设我从包含以下内容的任何文件中读取了一行:
line = "Name Age Group Class Profession"
Now I have to insert 3rd Field(Group) 3 times more in the same line before Class field. It means the output line should be:
现在我必须在 Class 字段之前的同一行中插入 3rd Field(Group) 3 次。这意味着输出行应该是:
output_line = "Name Age Group Group Group Group Class Profession"
I can retrieve 3rd field easily (using splitmethod), but please let me know the easiest way of inserting into the string?
我可以轻松检索第三个字段(使用split方法),但请告诉我插入字符串的最简单方法?
回答by Micha? Niklas
line='Name Age Group Class Profession'
arr = line.split()
for i in range(3):
arr.insert(2, arr[2])
print(' '.join(arr))
回答by Tim Pietzcker
There are several ways to do this:
做这件事有很多种方法:
One way is to use slicing:
一种方法是使用切片:
>>> a="line=Name Age Group Class Profession"
>>> b=a.split()
>>> b[2:2]=[b[2]]*3
>>> b
['line=Name', 'Age', 'Group', 'Group', 'Group', 'Group', 'Class', 'Profession']
>>> a=" ".join(b)
>>> a
'line=Name Age Group Group Group Group Class Profession'
Another would be to use regular expressions:
另一种方法是使用正则表达式:
>>> import re
>>> a=re.sub(r"(\S+\s+\S+\s+)(\S+\s+)(.*)", r"", a)
>>> a
'line=Name Age Group Group Group Group Class Profession'
回答by bgporter
An important point that often bites new Python programmers but the other posters haven't made explicit is that strings in Python are immutable -- you can't evermodify them in place.
一个经常困扰 Python 新手但其他海报没有明确说明的重要一点是 Python 中的字符串是不可变的——你永远无法修改它们。
You need to retrain yourself when working with strings in Python so that instead of thinking, "How can I modify this string?" instead you're thinking "how can I create a new string that has some pieces from this one I've already gotten?"
在 Python 中处理字符串时,您需要重新训练自己,这样就不必思考“我如何修改这个字符串?” 相反,你在想“我怎样才能创建一个新的字符串,其中包含我已经得到的一些片段?”
回答by Ben Avnon
For the sake of future 'newbies' tackling this problem, I think a quick answer would be fitting to this thread.
为了将来解决这个问题的“新手”,我认为一个快速的答案将适合这个线程。
Like bgportersaid: Python strings are immutable, and so, in order to modify a string you have to make use of the pieces you already have.
就像bgporter所说:Python 字符串是不可变的,因此,为了修改字符串,您必须利用已有的部分。
In the following example I insert 'Fu'in to 'Kong Panda', to create 'Kong Fu Panda'
在下面的示例中,我插入'Fu'到'Kong Panda', 以创建'Kong Fu Panda'
>>> line = 'Kong Panda'
>>> index = line.find('Panda')
>>> output_line = line[:index] + 'Fu ' + line[index:]
>>> output_line
'Kong Fu Panda'
In the example above, I used the index value to 'slice' the string in to 2 substrings: 1 containing the substring before the insertion index, and the other containing the rest. Then I simply add the desired string between the two and voilà, we have inserted a string inside another.
在上面的示例中,我使用索引值将字符串“切片”为 2 个子字符串:1 个包含插入索引之前的子字符串,另一个包含其余的子字符串。然后我简单地在两者之间添加所需的字符串,瞧,我们已经在另一个字符串中插入了一个字符串。
Python's slice notationhas a great answer explaining the subject of string slicing.
Python 的切片符号有一个很好的答案来解释字符串切片的主题。
回答by Sim Mak
I know it's malapropos, but IMHO easy way is:
我知道这是不合理的,但恕我直言,简单的方法是:
def insert (source_str, insert_str, pos):
return source_str[:pos]+insert_str+source_str[pos:]
回答by Homam Bahrani
I had a similar problem for my DNA assignment and I used bgporter's advice to answer it. Here is my function which creates a new string...
我的 DNA 分配也有类似的问题,我使用 bgporter 的建议来回答它。这是我创建一个新字符串的函数...
def insert_sequence(str1, str2, int):
""" (str1, str2, int) -> str
Return the DNA sequence obtained by inserting the
second DNA sequence into the first DNA sequence
at the given index.
>>> insert_sequence('CCGG', 'AT', 2)
CCATGG
>>> insert_sequence('CCGG', 'AT', 3)
CCGATG
>>> insert_sequence('CCGG', 'AT', 4)
CCGGAT
>>> insert_sequence('CCGG', 'AT', 0)
ATCCGG
>>> insert_sequence('CCGGAATTGG', 'AT', 6)
CCGGAAATTTGG
"""
str1_split1 = str1[:int]
str1_split2 = str1[int:]
new_string = str1_split1 + str2 + str1_split2
return new_string
回答by Toothpick Anemone
Implementation
执行
The functions below will allow one to insert one string into another string:
下面的函数将允许将一个字符串插入另一个字符串:
def str_insert(from_me, into_me, at):
"""
Inserts the string <from_me> into <into_me>
Input <at> must be an integer index of <into_me> or a substring of <into_me>
Inserts <from_me> AFTER <at>, not before <at>
Inputs <from_me> and <into_me> must have working __str__ methods defined.
This is satisfied if they already are strings.
If not already strings, <from_me>, <into_me> are converted into strings.
If you try to insert an empty string, that's fine, and the result
is no different from the original.
In order to insert 'from_me' after nothing (insert at the beginning of the string) use:
at = '' or at = 0
"""
try:
return str_insert_or_raise(from_me, into_me, at)
except ValueError as err:
serr = str(err)
if (str_insert_or_raise.__name__ in serr) and 'not found' in serr and '<at>' in serr:
# if can't find where to insert stuff, don't bother to insert it
# use str_insert_or_raise if you want an exception instead
return into_me
else:
raise err
##############################################################
def str_insert_or_raise(from_me, into_me, at):
"""
Inserts the string <from_me> into <into_me>
Inserts <from_me> AFTER <at>, not before <at>
Input <at> must be an integer index of <into_me> or a substring of <into_me>
If <at> is the string '15', that substring will be searched for,
'15' will not be interpreted as an index/subscript.
Inputs <from_me> and <into_me> must have working __str__ methods defined.
If not already strings, <from_me>, <into_me> are converted into strings.
If you try to insert something, but we cannot find the position where
you said to insert it, then an exception is thrown guaranteed to at least
contain the following three substrings:
str_insert_or_raise.__name__
'not found'
'<at>'
"""
try:
if isinstance(at, int):
return str_insert_by_int(from_me, into_me, at)
# Below, the calls to str() work fine if <at> and <from_me> are already strings
# it makes them strings if they are not already
return str_insert_by_str(str(from_me), str(into_me), str(at))
except ValueError as err:
serr = str(err)
if 'empty string' in serr:
return into_me # We allow insertion of the empty string
elif ("<at>" in serr) and 'not found' in serr:
msg_start = "In " + str_insert_or_raise.__name__ + ": "
msg = [msg_start, "\ninput ", "<at> string", " not found in ", "<into_me>",
"\ninput <", str(at) , "> not found in <", str(into_me), ">"]
msg = ''.join(msg)
raise ValueError(msg) from None
else:
raise err
#############################################################
def str_insert_by_str(from_me, into_me, at):
"""
Inserts the string <from_me> into <into_me>
puts 'from_me' AFTER 'at', not before 'at'
For example,
str_insert_or_raise(at = '2', from_me = '0', into_me = '123')
puts the zero after the 2, not before the 2
The call returns '1203' not '1023'
Throws exceptions if input arguments are not strings.
Also, if <from_me> is empty or <at> is not a substring of <into_me> then
an exception is raised.
For fewer exceptions, use <str_insert_or_raise> instead.
"""
try:
s = into_me.replace(at, at + from_me, 1)
except TypeError as terr: # inputs to replace are not strings
msg_list = ['Inputs to function ', str_insert_by_str.__name__, '() must be strings']
raise TypeError(''.join(msg_list)) from None
# At the end of call to replace(), the '1' indicates we will replace
# the leftmost occurrence of <at>, instead of every occurrence of <at>
if (s == into_me): # <at> string not found and/or <from_me> is the empty string
msg_start = "In " + str_insert_by_str.__name__ + ": "
if from_me == '':
msg = ''.join([msg_start, "attempted to insert an empty string"])
raise ValueError(msg) from None
raise ValueError(msg_start, "Input <at> string not found in <into_me>.",
"\nUnable to determine where you want the substring inserted.") from None
return s
##################################################
def str_insert_by_int(from_me, into_me, at):
"""
* Inserts the string <from_me> into <into_me> at integer index <at>
* throws exceptions if input arguments are not strings.
* Also, throws an exception if you try to insert the empty string
* If <at> is less than zero, <from_me> gets placed at the
beginning of <into_me>
* If <at> is greater than the largest index of <into_me>,
<from_me> gets placed after the end of <into_me>
For fewer exceptions, use <str_insert_or_raise> instead.
"""
at = into_me[:(at if at > 0 else 0)]
return str_insert_by_str(from_me, into_me, at)
Usage
用法
The code below demonstrates how to call the str_insertfunction given earlier
下面的代码演示了如何调用str_insert前面给出的函数
def foo(*args):
return args
F = 'F. '
s = 'Using the string \'John \' to specify where to make the insertion'
result = str_insert(from_me = F, into_me ='John Kennedy', at ='John ')
print(foo('\n\n', s, '\n', result))
s = 'Using an int returned by find(\'Ken\') to specify where to make the insertion'
index = 'John Kennedy'.find('Ken') # returns the position of the first letter of 'Ken', not the last letter
result = str_insert(from_me = F, into_me ='John Kennedy', at = index)
print(foo('\n\n', s, '\n', result))
s = 'Using an int (5) to specify where to make the insertion.'
result = str_insert(from_me = F, into_me ='John Kennedy', at = 5)
print(foo('\n\n', s, '\n', result))
s = "Looking for an 'at' string which does not exist"
result = str_insert(from_me = F, into_me ='John Kennedy', at ='x')
print(foo('\n\n', s, '\n', result))
s = ''.join(["Looking for the empty string.",
"\nFind one immediately at the beginning of the string"])
result = str_insert(from_me = F, into_me ='John Kennedy', at = '')
print(foo('\n\n', s, '\n', result))
s = "Insert an empty string at index 3. No visible change"
result = str_insert(from_me = '', into_me = 'John Kennedy', at = 3)
print(foo('\n\n', s, '\n', result))
for index in [-5, -1, 0, 1, 997, 999]:
s = "index " + str(index)
result = str_insert(from_me = F, into_me = 'John Kennedy', at = index)
print(foo('\n\n', s, '\n', result))
Warning About Lack of Ability to Modify In-Place
关于缺乏就地修改能力的警告
None of the functions above will modify a string "in-place." The functions each return a modified copy of the string, but the original string remains intact.
上述函数都不会“就地”修改字符串。每个函数都返回字符串的修改副本,但原始字符串保持不变。
For example,
例如,
s = ''.join(["Below is what we get when we forget ",
"to overwrite the string with the value",
" returned by str_insert_or_raise:"])
examp_str = 'John Kennedy'
str_insert('John ', F, examp_str)
print(foo('\n\n', s, '\n', examp_str))
# examp_str is still 'John Kennedy' without the F
回答by pavan kumar
Answer for Insert characters of string in other string al located positions
str1 = "ibuprofen"
str2 = "MEDICAL"
final_string=""
Value = 2
list2=[]
result=[str1[i:i+Value] for i in range(0, len(str1), Value)]
count = 0
for letter in result:
if count < len(result)-1:
final_string = letter + str2[count]
list2.append(final_string)
elif ((len(result)-1)==count):
list2.append(letter + str2[count:len(str2)])
break
count += 1
print(''.join(list2))

