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

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

我是新手,正在尝试找到在字符串的开头和结尾插入多个L的方法。所以如果我有一个字符串说

“今天早上我把蛋糕放在哪里?”

我想在开始处插入1 L,在结尾处插入2 L,所以看起来像:“今天早上我在哪里把纸杯蛋糕放到了LL”,我该怎么做。谢谢

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

“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


回答 0

字符串是不可变的,因此您不能在现有字符串中插入字符。您必须创建一个新字符串。您可以使用字符串串联来执行所需的操作:

yourstring = "L" + yourstring + "LL"

请注意,您还可以L使用乘法来创建具有n s 的字符串:

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

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:

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

回答 1

为了完整性以及其他答案:

yourstring = "L%sLL" % yourstring

或者,更向前兼容Python 3.x:

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

For completeness along with the other answers:

yourstring = "L%sLL" % yourstring

Or, more forward compatible with Python 3.x:

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

回答 2

您还可以使用join:

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

结果:

>>> yourstring
'LyourstringLL'

You can also use join:

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

Result:

>>> yourstring
'LyourstringLL'

回答 3

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

第二个位置的调用字符:

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

带有起始和结束位置的通话范围:

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

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

>>> s[:6]
'012345'

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

>>> s[4:]
'456789'

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

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

s等于s[:]

对于您的问题,您可以使用所有字符串,即

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

或者如果"L"是其他字符串(例如,我称其为l),则可以使用该代码:

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

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.

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

Also s is equivalent to 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:

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

回答 4

在Python 3.6及更高版本中,将C2H5OH的答案添加进去,您可以使用格式字符串使其更加简洁:

s = "something about cupcakes"
print(f"L{s}LL")

Adding to C2H5OH’s answer, in Python 3.6+ you can use format strings to make it a bit cleaner:

s = "something about cupcakes"
print(f"L{s}LL")

回答 5

假设我们有一个名为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"

Let’s say we have a string called 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"

回答 6

我正在通过用户输入呈现URL。如果用户输入包含两个单词的字符串,我想在中间用+打印该单词

key = input("Enter the product :")

URL = "http://exmaple.com/"

print (URL)

User input: iphone 11

对于上面的代码,我得到的URL为“ http://exmaple.com/iphone 11”

但我想将URL打印为“ http://exmaple.com/iphone+11

I am rendering a URL by user input. There if user enter a string with two words I want to print the word with + in between

Example

key = input("Enter the product :")

URL = "http://exmaple.com/"

print (URL)

User input: iphone 11

For the above code, I get a URL as “http://exmaple.com/iphone 11″

But I want to print the URL as “http://exmaple.com/iphone+11