Python - Insert characters at start and end of a string
Last Updated :
06 Dec, 2024
In Python programming, a String is an immutable datatype. But sometimes there may come a situation where we may need to manipulate a string, say add a character to a string.
Let’s start with a simple example to insert characters at the start and end of a String in Python.
Python
s = "For"
s2 = "Geeks"
r = s2 + s + s2
print(r)
Output:
GeeksForGeeks
Explanation: The word "Geeks" is inserted at the start and the end of the string "For". As there is no white space in any of the strings, the words are joined together as one word.
Using String Concatenation
The most common way to join two or more strings is using the String Concateniation method, which used the "+" operator to combine the strings
Python
s = "GeeksForGeeks"
# inserting same characters
r = "Hello" + s + "Hello"
print(r)
# inserting different characters
r = "Hello" + s + "World"
print(r)
Output:
HelloGeeksForGeeksHello
HelloGeeksForGeeksWorld
Using join() Function
The join() function is also used to concatenate two or more strings in Python. It takes an iterable, that is a list or a tuple as the parameters and join them in the given order.
Python
s = "GeeksForGeeks"
# inserting same characters
r = "".join(["Hello", s, "Hello"])
print(r)
# inserting different characters
r = "".join(["Hello", s, "World"])
print(r)
Output:
HelloGeeksForGeeksHello
HelloGeeksForGeeksWorld
Using f-String
The f-string in Python is used in string formatting. It directly embeds the expression inside the curly braces.
Python
s = "GeeksForGeeks"
# inserting same characters
r = f"Hello{s}Hello"
print(r)
# inserting different characters
r = f"Hello{s}World"
print(r)
Output:
HelloGeeksForGeeksHello
HelloGeeksForGeeksWorld
The Python format() function is an inbuilt function used to format strings. Unline f-string, it explicitly takes the expression as arguments.
Python
s = "GeeksForGeeks"
# inserting same characters
r = "Hello{}Hello".format(s)
print(r)
# inserting different characters
r = "Hello{}World".format(s)
print(r)
Output:
HelloGeeksForGeeksHello
HelloGeeksForGeeksWorld
Similar Reads
Split String into List of characters in Python We are given a string and our task is to split this string into a list of its individual characters, this can happen when we want to analyze or manipulate each character separately. For example, if we have a string like this: 'gfg' then the output will be ['g', 'f', 'g'].Using ListThe simplest way t
2 min read
Iterate over characters of a string in Python In this article, we will learn how to iterate over the characters of a string in Python. There are several methods to do this, but we will focus on the most efficient one. The simplest way is to use a loop. Letâs explore this approach.Using for loopThe simplest way to iterate over the characters in
2 min read
Get Last N characters of a string - Python We are given a string and our task is to extract the last N characters from it. For example, if we have a string s = "geeks" and n = 2, then the output will be "ks". Let's explore the most efficient methods to achieve this in Python.Using String Slicing String slicing is the fastest and most straigh
2 min read
Reverse Alternate Characters in a String - Python Reversing alternate characters in a string involves rearranging the characters so that every second character is reversed while maintaining the original order of other characters. For example, given the string 'abcde', reversing the alternate characters results in 'ebcda', where the first, third and
3 min read
Replace a String character at given index in Python In Python, strings are immutable, meaning they cannot be directly modified. We need to create a new string using various methods to replace a character at a specific index. Using slicingSlicing is one of the most efficient ways to replace a character at a specific index.Pythons = "hello" idx = 1 rep
2 min read