168

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

8 Answers 8

198

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)
Sign up to request clarification or add additional context in comments.

Comments

39

You can also use join:

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

Result:

>>> yourstring
'LyourstringLL'

4 Comments

10 years later and i still don't get why adding an extra set of parentheticals to the join method allows this to work.
cuz tuple is iterable.
@VikingsDood you give the join function a list or a tuple or any other iterable, and it will append all of those strings onto the originial string. So "hi".join(["hello","goodbye"]) will become hihello and finally return hihellogoodbye. In Akavall's answer, an empty string is used in the place of "hi". The join function needs to be able to iterate over what you give it, so you need to put them in an iterable. If you omit the paretheses, you are just giving it three separate strings as arguments.
That makes sense, it just needs to have an iterable object space to process the data. It does not necessarily have to be a tuple.
37

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 Comments

For the second example, using only one argument for .format the '0' is unnecessary, right?
@quapka: It is necessary if the code needs to be compatible with Python 2.6
15

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)

Comments

15

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")

Comments

3

you can use f strings for this

foo = "where did I put my cupcake this morning"
bar = 'L'
foobar = f'{bar*10}{foo}'
print(foobar)

you can replace 10 by how many times you want to put L's in your string for end also you can do the same

foo = "where did I put my cupcake this morning"
bar = 'L'
foobar = f'{bar*10}{foo}{bar*10}'
print(foobar)

Comments

2

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"

Comments

2

You can easily prepend and append strings by using f-strings:

text = "where did I put my cupcake this morning"
prepend = "L"
append = "LL"

print(f"{prepend}{text}{append}")

# Result: "Lwhere did I put my cupcake this morningLL"

Or you can build a function that gives you more flexibility:

def prepend_and_append(text: str, prepend_char: str, prepend_times: int, append_char: str, append_times: int) -> str:
    return f"{prepend_char * prepend_times}{text}{append_char * append_times}"

print(prepend_and_append(text, "L", 1, "L", 2))

# Result: "Lwhere did I put my cupcake this morningLL"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.