Add character to String in Python

Add character to String in Python

Strings are considered as a sequence of characters in programming. In Python, string objects are considered immutable. What this means is that string objects cannot be changed once created, and a new string is created with a different block of memory when we edit a string.

In this article, we will discuss how to add a character to string in Python.

Using the + operator

Apart from its arithmetic use, the + can also be used as a concatenation operator. It can combine one or more strings or characters based on our desired position. Given its flexibility and simple logic, this method is used very commonly for adding characters in a string.

In the following code, we will add a character at different positions in a string.

Output:

Python
Python
Python

In the above code, we added a character to a string at the start, end, and some particular index. While adding a character at some index, we simply slice the string into two substrings. One substring contains the part before the index and the other contains after the index. We simply concatenate the two substrings with the character in between to get the final result.

Using the join() function

The join() function can work on iterable objects for combining their constituents based on a specified separator character or string. It can be used to add a character in a string at our desired position.

For example,

Output:

Python
Python
Python

In the above example, we specify the separator character for the join() function as an empty character to avoid any space.

Using the % operator

In Python, we can use many methods to format strings and get the output in our desired form. We can use the % operator to format and add characters to a string.

For example,

Output:

Python
Python
Python

Using the format() function

The format() function is also used for string formatting in Python. It uses the same pattern to specify the format to add character to a string as discussed above.

For example,

Output:

Python
Python
Python

If any error is encountered in the above method, try changing the format to {0}.

Using the f-strings

The f-strings were introduced in Python 3 and are very popular when it comes to formatting strings in Python. It has an advantage over the % operator and format() function in terms of its speed and simplicity. We just have to prefix the string with f to use this method.

We can add a character to a string using this as shown below.

Output:

Python
Python
Python

That’s all how to add character to String in Python.

Was this post helpful?

Related Posts

  • 19 May

    Remove First and Last Character of String in Python

    Table of ContentsHow to Remove First and Last Character of string in python?Remove First and Last Character of string Using For LoopRemove First and Last Character of string Using SlicingRemove First and Last word of string in PythonConclusionWas this post helpful? We use strings in python to handle text data. For processing the data, we […]

  • 19 May

    Remove HTML Tags From String in Python

    Table of ContentsRemove HTML tags from string in python Using Regular ExpressionsRemove HTML tags from string in python Using the lxml ModuleRemove HTML tags from string in python Using the Beautifulsoup ModuleConclusionWas this post helpful? While collecting data, we often need to process texts with HTML tags. In this article, we will discuss different ways […]

  • 19 May

    Remove xa0 from String in Python

    Table of ContentsHow to remove xa0 from string in python?Using the decode() function to remove xa0 from string in python.Using the re library to remove xa0 from string in python.Using the normalize() function from the unicodedata library to remove xa0 from string in python.Using the get_text() function from the BeautifulSoup library to remove xa0 from […]

  • 18 May

    Convert bool to String in Python

    Table of ContentsUsing the str() function to convert bool to string in Python.Using string formatting with the help of the % operator to convert bool to string in Python.Using string formatting with the help of the format() function to convert bool to string in Python.Using f-strings to convert bool to string in Python.Was this post […]

  • 18 May

    Convert UUID to String in Python

    Table of ContentsUUID in PythonWays to convert UUID to string in PythonUsing the str() function to convert UUID to String in PythonUsing the hex attribute to convert UUID to String in PythonUsing the urn attribute to convert UUID to String in PythonConclusionWas this post helpful? In this post, we will see how to convert UUID […]

  • 18 May

    How to Replace Word in String in Python

    Table of ContentsUsing the replace() Function to replace word in string in PythonUsing the re.Sub() Function to replace word in string in PythonUsing the re.subn() function to replace word in string in PythonConclusionWas this post helpful? In this tutorial, we will to how to replace word in string in Python. Strings represent a sequence of […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.