在字符串的开头和结尾插入字符

时间:2021-04-19 21:41:36

I am new and trying to find a way to insert a number of L's at the beginning and end of a string. So if I have a string which says

我是新来的,我想找到一种方法在字符串的开头和结尾插入一些L。如果我有一个字符串

"where did I put my cupcake this morning"

"今天早上我把蛋糕放在哪儿了"

And I want to insert 1 L at the start and 2 L's at the end, so it looks like: "Lwhere did I put my cupcake this morningLL" How do I do this. thank you

我想在开头插入一个L,在结尾插入两个L,所以看起来是这样的:“今天早上我把蛋糕放在哪里了?”谢谢你!

5 个解决方案

#1


89  

Strings are immutable so you can't insert characters into an existing string. You have to create a new string. You can use string concatenation to do what you want:

字符串是不可变的,所以您不能将字符插入到现有的字符串中。你必须创建一个新的字符串。你可以使用字符串连接来做你想做的:

yourstring = "L" + yourstring + "LL"

Note that you can also create a string with n Ls by using multiplication:

注意,您还可以使用乘法创建一个具有n个Ls的字符串:

m = 1
n = 2
yourstring = ("L" * m) + yourstring + ("L" * n)

#2


19  

For completeness along with the other answers:

为了完整性和其他答案:

yourstring = "L%sLL" % yourstring

Or, more forward compatible with Python 3.x:

或与Python 3.x更向前兼容:

yourstring = "L{0}LL".format(yourstring)

#3


17  

You can also use join:

你也可以使用join:

yourstring = ''.join(('L','yourstring','LL'))

Result:

结果:

>>> yourstring
'LyourstringLL'

#4


1  

If you want to insert other string somewhere else in existing string, you may use selection method below.

如果您想在现有字符串的其他位置插入其他字符串,可以使用下面的选择方法。

Calling character on second position:

在第二个位置调用字符:

>>> s = "0123456789"
>>> s[2]
'2'

Calling range with start and end position:

起止位置调用范围:

>>> s[4:6]
'45'

Calling part of a string before that position:

在该位置之前调用字符串的部分:

>>> s[:6]
'012345'

Calling part of a string after that position:

在该位置之后调用字符串的部分:

>>> s[4:]
'456789'

Inserting your string in 5th position.

将您的字符串插入第5位。

>>> s = s[:5] + "L" + s[5:]
>>> s
'01234L56789'

Also s is equivalent to s[:].

s也等于s[:]。

With your question you can use all your string, i.e.

对于你的问题,你可以用你所有的字符串,例如。

>>> s = "L" + s + "LL"

or if "L" is a some other string (for example I call it as l), then you may use that code:

或者如果“L”是另一个字符串(例如,我称它为L),那么您可以使用该代码:

>>> s = l + s + (l * 2)

#5


0  

Let's say we have a string called yourstring:

假设我们有一个字符串叫做yourstring:

for x in range(0, [howmanytimes you want it at the beginning]):
    yourstring = "L" + yourstring
for x in range(0, [howmanytimes you want it at the end]):
    yourstring += "L"

#1


89  

Strings are immutable so you can't insert characters into an existing string. You have to create a new string. You can use string concatenation to do what you want:

字符串是不可变的,所以您不能将字符插入到现有的字符串中。你必须创建一个新的字符串。你可以使用字符串连接来做你想做的:

yourstring = "L" + yourstring + "LL"

Note that you can also create a string with n Ls by using multiplication:

注意,您还可以使用乘法创建一个具有n个Ls的字符串:

m = 1
n = 2
yourstring = ("L" * m) + yourstring + ("L" * n)

#2


19  

For completeness along with the other answers:

为了完整性和其他答案:

yourstring = "L%sLL" % yourstring

Or, more forward compatible with Python 3.x:

或与Python 3.x更向前兼容:

yourstring = "L{0}LL".format(yourstring)

#3


17  

You can also use join:

你也可以使用join:

yourstring = ''.join(('L','yourstring','LL'))

Result:

结果:

>>> yourstring
'LyourstringLL'

#4


1  

If you want to insert other string somewhere else in existing string, you may use selection method below.

如果您想在现有字符串的其他位置插入其他字符串,可以使用下面的选择方法。

Calling character on second position:

在第二个位置调用字符:

>>> s = "0123456789"
>>> s[2]
'2'

Calling range with start and end position:

起止位置调用范围:

>>> s[4:6]
'45'

Calling part of a string before that position:

在该位置之前调用字符串的部分:

>>> s[:6]
'012345'

Calling part of a string after that position:

在该位置之后调用字符串的部分:

>>> s[4:]
'456789'

Inserting your string in 5th position.

将您的字符串插入第5位。

>>> s = s[:5] + "L" + s[5:]
>>> s
'01234L56789'

Also s is equivalent to s[:].

s也等于s[:]。

With your question you can use all your string, i.e.

对于你的问题,你可以用你所有的字符串,例如。

>>> s = "L" + s + "LL"

or if "L" is a some other string (for example I call it as l), then you may use that code:

或者如果“L”是另一个字符串(例如,我称它为L),那么您可以使用该代码:

>>> s = l + s + (l * 2)

#5


0  

Let's say we have a string called yourstring:

假设我们有一个字符串叫做yourstring:

for x in range(0, [howmanytimes you want it at the beginning]):
    yourstring = "L" + yourstring
for x in range(0, [howmanytimes you want it at the end]):
    yourstring += "L"