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.

Related Posts

  • Get variable name as String in Python
    01 October

    How to get variable name as String in Python

    Table of ContentsWhat are variable names in Python?How to get a variable name as a string in Python?In Python 2.xIn Python 3.x In Python, there are several ways to convert a string to a variable name, but the reverse process is not exactly utilized so often in the programming world. This article focuses on how […]

  • Python hex to bytes
    31 May

    Convert Hex to bytes in Python

    Table of ContentsUse codecs.decode() function to Convert Hexadecimal value to Byte in PythonUse bytes.fromhex() function to Convert Hexadecimal value to Byte in PythonUse the binascii Module to Convert Hexadecimal value to Byte in Python 💡 Outline You can use codecs’s decode() function to convert Hex to bytes in python. This method works for Python 2.x […]

  • Remove commas from String in Python
    23 May

    Remove comma from String in Python

    Table of ContentsUsing replace() function to remove comma from string in PythonUsing re or RegEx package function to remove comma from string in Python 💡 Outline You can remove comma from string in python by using string’sreplace() method. Syntax: [crayon-61749e10c2d64760433473/] In this tutorial, we will take a sample string with a couple of commas in […]

  • Python find number in String
    16 May

    Python find number in String

    Table of ContentsUse of re.findall() functionUse of List Comprehension, split() and isdigit() functionsUse of split() and append() functionsUse of Numbers from String Library Extracting specific letters, phrases, words, and some specific characters is a very generic thing to do in the world of programming. In all these tasks, extraction of numbers and their occurrences from […]

  • 13 April

    How to Format Float to 2 Decimal Places in Python?

    In this article you will learn - How to Format a Float With Two Decimal Places in Python?

  • 11 April

    Python Remove Newline from List

    Table of ContentsIntroduction✨ Method 1: Using the replace() method✨ Method 2: Using re.sub()✨ Method 3: Using strip()✨ Method 4: Slice the List Elements✨ Method 5: Using List Comprehension and Splitlines Function✨ Method 6: Using str.maketrans(x,y) and str.translate(map)Conclusion 🎯 Objective: In this article, we will discuss the methods to remove ‘\n’ or newline characters from a […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.