Python Splice String

We can perform different operations on objects like strings, lists, and tuples in python. In this article, we will discuss how to splice a string in Python.

What Is Python String Splicing?

Python string splicing is similar to string concatenation with some additional features such as the replacement of characters. We can perform a python splicing operation on a string to create a larger string. For this, we use the + operator as the python splice operator.

If we are given three smaller strings str1, str2, and str3, the syntax to perform string splicing will be as follows.

myStr=str1+str2+str3

In the above code, str1, str2, and str3 are spliced using the + operator to create the output string myStr. In essence, we join all the smaller strings to create a larger string during the string splicing operation.

Python Splice String

To splice multiple strings into a single string, we can use the string splicing operator as shown below.

str1="Avid"
str2="Python"
str3= "Tutorials"
myStr=str1+str2+str3
print("The first string is:",str1)
print("The second string is:",str2)
print("The third string is:",str3)
print("The spliced string is:",myStr)

Output:

The first string is: Avid
The second string is: Python
The third string is: Tutorials
The spliced string is: AvidPythonTutorials

In the above example, we have created three strings "Avid", "Python", and "Tutorials". Then, we spliced them into a single string using the + operator.

Splice Two Parts of a String

To splice two parts of a string, we can simply concatenate them using the + operator as shown below.

str1="Avid"
str2="Python"
myStr=str1+str2
print("The first part of string is:",str1)
print("The second part of string is:",str2)
print("The spliced string is:",myStr)

Output:

The first part of string is: Avid
The second part of string is: Python
The spliced string is: AvidPython

Python Splice String at the Start of Another String

To splice a string at the start of another string, you can simply add the new string to an existing string using the splice operator as shown below.

str1="Python"
str2="Avid"
myStr=str2+str1
print("The original string is:",str1)
print("The new string is:",str2)
print("The spliced string is:",myStr)

Output:

The original string is: Python
The new string is: Avid
The spliced string is: AvidPython

In this example, we have spliced the string "Avid" to the start of the existing string "Python" using the + operator.

Splice String at the End of Another String

Instead of splicing at the start of a python string, we can also splice a string at the end of a string using the + operator as shown below.

str1="Avid"
str2="Python"
myStr=str1+str2
print("The original string is:",str1)
print("The new string is:",str2)
print("The spliced string is:",myStr)

Output:

The original string is: Avid
The new string is: Python
The spliced string is: AvidPython

In this example, we have spliced the string “Python” to the end of the existing string “Avid” using the + operator.

Suggested Reading: String Manipulation in Python

Python Splice String at Certain Character

To splice a string at a certain character, we will use the following approach.

Suppose that we want to splice a string at the index splice_index of another string. For this, we will first create two substrings from the original string. The first substring contains characters from the start of the string to the index splice_index-1. The second substring contains characters from the index splice_index till the end.

Now, we will use the splice operator + to splice the given string into the original string at the certain character given at index splice_index as shown below.

str1="Avid Tutorial"
str2="Python"
splice_index=str1.index(" ")
first_part=str1[:splice_index]
second_part=str1[splice_index:]
myStr=first_part+str2+second_part
print("The original string is:",str1)
print("The new string is:",str2)
print("The spliced string is:",myStr)

Output:

The original string is: Avid Tutorial
The new string is: Python
The spliced string is: AvidPython Tutorial

In the above example, we have spliced the string "Python" at the position of the whitespace character in the string "Avid Tutorial". For this, we first obtained the index of the whitespace character in the original string using the index() method. The index() method, when invoked on a string, takes a character as its input argument and returns the index of the character.

After obtaining the index of the whitespace character, we used python indexing along with the + operator to splice the string "Python" into the string "Avid Tutorial". After execution, we get the output string “AvidPython Tutorial".

Splice String by Replacing a Character

To splice a string into another string by replacing a character at a given index, we will use the following approach.

We will first create three substrings from the original string. The first substring contains characters from the start of the string to the index splice_index-1. The second substring contains the character at the index splice_index of the original string. The third substring contains characters from the index splice_index+1 till the end.

Now, we will use the splice operator + to splice the given string into the substrings of the original string by replacing the character given at index splice_index as shown below.

str1="Avid Tutorial"
str2="Python"
splice_index=str1.index(" ")
first_part=str1[:splice_index]
second_part=str1[splice_index]
third_part=str1[splice_index+1:]
myStr=first_part+str2+third_part
print("The original string is:",str1)
print("The new string is:",str2)
print("The spliced string is:",myStr)

Output:

The original string is: Avid Tutorial
The new string is: Python
The spliced string is: AvidPythonTutorial

In the above example, we have replaced the whitespace character with the string "Python" in the original string “Avid Tutorial". For this, we first calculated the index of the whitespace character. Then, we split the original string into three parts. The first part contains all the characters before the whitespace character. The second part contains the whitespace character. The third part contains all the characters after the whitespace character.

Finally, we concatenated the first part of the original string, the new string, and the third part of the original string using the + operator to produce the output.

Python Splice String by Replacing a Substring

Instead of a character, suppose that we want to replace a substring from the original string and insert a new string using string splicing. Let us consider that we need to replace the substring from the index i to j of a given string using the python splice operation. For this, we will use the following approach.

First, we will first create three substrings from the original string. The first substring contains characters from the start of the string to the index i-1. The second substring contains characters from index i to index j. The third substring contains characters from the index j+1 till the end. 

Now that we have to replace the characters from the index i to j in the original string with a new substring, we will use the splice operator + to splice the given string into the substrings of the original string by omitting the characters from index i to j as shown below.

str1="Avid Go Tutorial"
str2="Python"
splice_index=str1.index("Go")
first_part=str1[:splice_index]
second_part=str1[splice_index:splice_index+2]
third_part=str1[splice_index+2:]
myStr=first_part+str2+third_part
print("The original string is:",str1)
print("The new string is:",str2)
print("The spliced string is:",myStr)

Output:

The original string is: Avid Go Tutorial
The new string is: Python
The spliced string is: Avid Python Tutorial

In the above example, we have replaced the substring "Go" by "Python" in the original string. For this, we first split the original string into three parts.

The first part contains all the characters before the substring "Go". The second part contains the substring "Go". The third part contains all the characters after the substring.

Finally, we concatenated the first part of the original string, the new string, and the third part of the original string using the + operator to obtain the output.

Python String Splicing by a Dash

In the above examples, we have directly joined the strings. Instead, we can also use other characters for string splicing. For instance, we can splice given strings by the dash character as shown below.

str1="Avid"
str2="Python"
str3= "Tutorials"
dash="—"
myStr=str1+dash+str2+dash+str3
print("The first string is:",str1)
print("The second string is:",str2)
print("The third string is:",str3)
print("The spliced string is:",myStr)

Output:

The first string is: Avid
The second string is: Python
The third string is: Tutorials
The spliced string is: Avid—Python—Tutorials

In the above example, we have used the dash character in between the strings to splice the strings by dash during concatenation.

We can also splice a string at a certain character using a dash as shown below.

str1="Avid Tutorial"
str2="Python"
dash="—"
splice_index=str1.index(" ")
first_part=str1[:splice_index]
second_part=str1[splice_index:]
myStr=first_part+dash+str2+dash+second_part
print("The original string is:",str1)
print("The new string is:",str2)
print("The spliced string is:",myStr)

Output:

The original string is: Avid Tutorial
The new string is: Python
The spliced string is: Avid—Python— Tutorial

Conclusion

In this article, we have discussed different ways to splice a string in Python. To learn more about python programming, you can read this article on python simplehttpserver. You might also like this article on sys.argv in python.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Leave a Reply