. Advertisement .
..3..
. Advertisement .
..4..
Computers are mostly used to store information and extract information from it. Python is one of the most powerful programming languages. We can use Python to get computers to do most of the routine processes. This blog is all about how to add character to string in Python.
To add character to string in python
To add a character in a string in Python, simply use the join() command. In Python, you can add character to a string by utilizing join(). So, without wasting any time, let’s take a look at an example of this.
var1 = "We are"
var1 = ' '.join((var1,'ITtutoria'))
print(var1)
Result:
We are ITtutoria
To include a character in a string in Python, simply use %. In Python, you can also include character in a String by utilizing %. So, without wasting any time, let’s take a look at an example of this.
var1 = "We are"
var1 = "We are %s" %'ITtutoria'
print(var1)
Result:
We are ITtutoria
Approach 1: Utilize join()
In Python, you can add character to a String by utilizing join(). So, without wasting any time, let’s take a look at an example of this.
var1 = "We are"
var1 = ' '.join((var1,'ITtutoria'))
print(var1)
Result:
We are ITtutoria
Approach 2: Utilize %
In Python, you can also include character in a String by utilizing %. So, without wasting any time, let’s take a look at an example of this.
var1 = "We are"
var1 = "We are %s" %'ITtutoria'
print(var1)
Result:
We are ITtutoria
Approach 3: Utilize format()
In Python, you can also include character in a String by utilizing format(). So, without wasting any time, let’s take a look at an example of this.
var1 = "We are"
var1 = "We are {}" .format('ITtutoria')
print(var1)
Result:
We are ITtutoria
Approach 4: Utilize f-strings
In Python, you can also include character in a String by utilizing f-strings. So, without wasting any time, let’s take a look at an example of this.
var1 = "We are"
var1 = f"We are {'ITtutoria'}"
print(var1)
Result:
We are ITtutoria
Approach 5: Using the +
operator
The plus sign (+) can be used as a concatenation operator in addition to its arithmetic function. Depending on the position you want it in, it can combine one or more strings or characters. This solution is frequently used to add characters to a string because of its adaptability and straightforward logic.
In the below program, we will add a character which is placed at different locations in a string.
s = "Pytho"
s = s + 'n' #Character at the end
print(s)
s1 = "Pyton"
s1 = s1[:3] + 'h' + s1[3:] #Character at particular pos
print(s1)
s2 = "ython"
s2 = 'P' + s2 #Character at the beginning
print(s2)
And we will receive the output:
Python
Python
Python
In the program above, a character was added to a string at the beginning, end, and several specific indexes. We just separate the string into two substrings in process of adding a character at some indexes. The portion before of the index is in a substring, and the portion after of the index is in another. To obtain the result, we only match two substrings with the between character.
Conclusion
Add character to string in Python is a confusing problem. We hope this blog has helped clear the air around how to do it. If you have more questions about this topic, please leave a comment below. Thank you for reading; we are always excited when one of our posts can provide useful information on a topic like this!
Let’s read this useful article to broaden your knowledge:
👇️ Read more: How To Count Occurrences of a Character in a String in Python?
Leave a comment