How to add a letter to string in python

  1. HowTo
  2. Python How-To's
  3. Add Character to String in Python

Created: February-10, 2021

  1. Add a Character to a String in Python Using the + Operator
  2. Add a Character to a String in Python Using the join() Method

This tutorial will explain the different methods to add a character to a string in Python. The string in Python is an immutable object, so all the methods which perform some action on a string return a new string. Suppose a user wants to add a character or number after randomly generating it to an existing string; this tutorial will look into different ways to add a character to a string.

Add a Character to a String in Python Using the + Operator

The + operator can concatenate two strings or a string and a character and returns a new string in Python. The example code below demonstrates how the + operator can be used to add a character to a string in Python.

print('The number is = ' + '2')

Output:

The number is = 2

Add a Character to a String in Python Using the join() Method

The string.join() method concatenates all the iterable object elements using the string as a separator between the elements. We can pass the string and the character as elements of a list to the string.join() method to add the desired character to the string.

The example code below demonstrates how to use the join() method to add a character to a string in Python:

print(" ".join(('The number is =', '2')))

Output:

The number is = 2

Related Article - Python String

  • Remove Commas From String in Python
  • Check a String Is Empty in a Pythonic Way
  • Convert a String to Variable Name in Python
  • Remove Whitespace From a String in Python
  • How to add a letter to string in python
    report this ad

    In this python tutorial, you will learn about how to append to a string python and, also we will check:

    • Append to a string python
    • Prepend to a string python
    • Insert to a string python
    • Append 0 to a string python
    • Append character to a string python
    • Python append to the beginning of a string in a loop
    • Add letter to a string python
    • Add variable to a string python
    • Add int to a string python
    • Append to the end of a string in python
    • How to append a new line to a string in python
    • How to append backslash to a string in python
    • How to append to an empty string in python
    • How to append double quotes to a string in python

    Let’s see how to append to a string python.

    In this example, we will use “+” operator to append to a string in python. The code below shows appending of two string.

    Example:

    s1 = "New"
    s2 = "Delhi"
    space = " "
    print(s1 + space + s2)

    You can refer to the below screenshot to see the output for append to a string python.

    How to add a letter to string in python
    Append to a string python

    This is how we can append to a string in Python.

    Read: String methods in Python with examples.

    Prepend to a string python

    Now, we will see how to prepend to a string python.

    Prepend to a string will add the element to a string. In this example, we have two strings and to get the output we will print my_string.

    Example:

    my_string = "Python"
    add_string = " is famous"
    my_string += add_string
    print("The string is : " + my_string)

    You can refer to the below screenshot to see the output for prepend to a string python

    How to add a letter to string in python
    Prepend to a string python

    This is how we can prepend a string in Python.

    Read: Remove character from string Python.

    Insert to a string python

    Here, we will see how to insert to a string python.

    • To insert into a string we will use indexing in python.
    • Inserting into a string returns the string with another string inserted into it at a given index.
    • In this example, we are inserting “New York” into “Los Angeles, Chicago” and it returns “Los Angeles, New York, Chicago”.

    Example:

    str1 = "Los Angeles, Chicago"
    str2 = "New York, "
    beg_substr = str1[:7]
    end_substr = str1[7:]
    my_str = beg_substr + str2 + end_substr
    print(my_str)

    This is how we can insert to a sting in Python.

    Read: Python write String to a file.

    Append 0 to a string python

    Let’s see how to append 0 to a string python.

    In this example, we will use rjust function for appending 0 to a string as it offers a single line way to perform the task.

    Example:

    t_str = 'New'
    N = 1
    res = t_str.rjust(N + len(t_str), '0')
    print("The string after adding zeros : " + str(res))

    You can refer to the below screenshot to see the output for append 0 to a string python.

    How to add a letter to string in python
    Append 0 to a string python

    The above Python code we can use to append 0 to a string in Python.

    Read: Python generate random number and string

    Append character to a string python

    Now, we will see how to append character to a string python.

    • To append a character to a string we will insert the character at an index of a string and it will create a new string with that character.
    • To insert a character we will use slicing a_str[:1] + “n” + a_str[1:] and the “+” operator is used to insert the desired character.

    Example:

    a_str = "Hello"
    a_str = a_str[:1] + "n" + a_str[1:]
    print(a_str)

    You can refer to the below screenshot to see the output for append character to a string python

    How to add a letter to string in python
    Append character to a string python

    This is how to append character to a string in Python.

    Read: How to convert a String to DateTime in Python

    Python append to beginning of a string in a loop

    Here, we will see Python append to the beginning of a string in a loop

    In this example, we will append to the beginning of a string using the for loop, and the “+” operator is used.

    Example:

    endstr = "People"
    my_list = ['Hello', 'to', 'all']
    for words in my_list:
      endstr = endstr + words
    print("The string is: " + endstr)

    You can refer to the below screenshot to see the output for python add to string in a loop.

    How to add a letter to string in python
    Python append to the beginning of a string in a loop

    The above code, we can use to append to beginning of a string in a loop in Python.

    Read: How to Convert Python string to byte array.

    Add letter to a string python

    Here, we will see how to add letter to a string python.

    To add letter to a string python we will use f”{str1}{l2}” and to get the output we will print(res).

    Example:

    str1 = "Python"
    l2 = "G"
    res = f"{str1}{l2}"
    print(res)

    You can refer to the below screenshot to see the output for add letter to a string python

    How to add a letter to string in python
    Add letter to a string python

    This is python code to add letter to a string in Python.

    Read: How to concatenate strings in python

    Add variable to a string python

    Let’s see how to add variable to a string python.

    In this example, we will use “+” operator to add a variable to a string.

    Example:

    var = "Guides"
    print("Python " + var + " for learning")

    You can refer to the below screenshot to see the output for add variable to a string python

    How to add a letter to string in python
    Add variable to a string python

    This is how to add variable to a string in Python.

    Read: Convert float to int Python

    Add int to a string python

    Now, we will see how to add int to a string python.

    Firstly, we will initialize the string and a number and by using the type conversion we will insert the number in the string, and then to get the output we will print.

    Example:

    t_str = "Python"
    t_int = 8 
    res = t_str + str(t_int) + t_str
    print("After adding number is  : " + str(res))

    You can refer to the below screenshot to see the output for add int to a string python

    How to add a letter to string in python
    Add int to a string python

    This is how to add int to a string in Python.

    Read: Python string formatting with examples

    Append to end of a string in python

    Here, we will see how to append to the end of a string in python

    In this example, we will use the “+” operator, and to get the output we will print(string3).

    Example:

    string1 = "Hello Wo"
    string2 = "rld"
    string3 = string1 + string2
    print(string3)

    You can refer to the below screenshot to see the output for append to the end of a string in python.

    How to add a letter to string in python
    Append to the end of a string in python

    The above code we can use to append to end of a string in python.

    Read: Python program to reverse a string

    How to append a new line to a string in python

    Let us see how to append a new line to a string in python

    In this example, to append a new line to a string we have specified the newline character by using “\n” which is also called an escape character. Hence, the string “to python” is printed in the next line.

    Example:

    my_string = "Welcome\n to python."
    print(my_string)

    You can refer to the below screenshot to see the output for how to append a new line to a string in python.

    How to add a letter to string in python
    How to append a new line to a string in python

    The above code we can use to append a new line to a string in python.

    Read: How to convert string to float in Python

    How to append backslash to a string in python

    Now, we will see how to append backslash to a string in python

    In this example, to append backslash to a string in python we have used the syntax “\\” to represent single backslash to a string.

    Example:

    my_str1 = "Hello"
    my_str2 = "\\"
    res = my_str1 + my_str2
    print(res)

    You can refer to the below screenshot to see the output for how to append backslash to a string in python.

    How to add a letter to string in python
    How to append backslash to a string in python

    The above code we can use to append backslash to a string in python.

    Read: Add string to list Python

    How to append to an empty string in python

    Let’s see how to append to an empty string in python.

    To append to an empty string in python we have to use “+” operator to append in a empty string.

    Example:

    str = ""
    res = str + "Hello World"
    print(res)

    You can refer to the below screenshot to see the output for how to append to an empty string in python.

    How to add a letter to string in python
    How to append to an empty string in python

    The above code we can use to append to an empty string in python.

    Read Python string to list

    How to append double quotes to a string in python

    Here, we will see how to append double quotes to a string in python.

    To append double quotes to a string in python we have to put the string in the single quotes.

    Example:

    double_quotes = '"Python"'
    print(double_quotes)

    You can refer to the below screenshot to see the output for how to append double quotes to a string in python.

    How to add a letter to string in python
    How to append double quotes to a string in python

    The above code we can use to append double quotes to a string in python.

    How to append to a string in python

    In python, to append a string in python we will use the ” + ” operator and it will append the variable to the existing string.

    Example:

    name = 'Misheil'
    salary = 10000
    print('My name is ' + name + 'and my salary is around' + str(salary))

    After writing the above Python code (how to append to a string in python), Ones you will print then the output will appear ” My name is Misheil and my salary is around 10000 “. Here, the ” + ” operator is used to append the variable. Also, you can refer to the below screenshot append to a string in python.

    How to add a letter to string in python
    How to append to a string in python

    Also read:

    • How to handle indexerror: string index out of range in Python
    • How to convert list to string in Python
    • Python String Functions
    • Python sort NumPy array
    • Python Count Words in File
    • Python convert binary to decimal + 15 Examples

    In this Python tutorial, we have learned about how to Append to a string python. Also, we covered these below topics:

    • Append to a string python
    • Prepend to a string python
    • Insert to a string python
    • Append 0 to a string python
    • Append character to a string python
    • Python append to the beginning of a string in a loop
    • Add letter to a string python
    • Add variable to a string python
    • Add int to a string python
    • Append to the end of a string in python
    • How to append a new line to a string in python
    • How to append backslash to a string in python
    • How to append to an empty string in python
    • How to append double quotes to a string in python
    • How to append to a string in python

    How to add a letter to string in python

    Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.

    How do you add text to a string in Python?

    To append a string to another in Python, use the += operator. Python += operator appends a string to another. It adds two values together and assigns the final value to a variable.

    How do you add a letter to the end of a string in Python?

    Use string concatenation to insert characters at the start and end of a string.
    string1 = "c".
    string2 = "ab" + string1 + "de".
    print(string2).

    How do you add a character to a string?

    Approach:.
    Get the Strings and the index..
    Create a new String..
    Insert the substring from 0 to the specified (index + 1) using substring(0, index+1) method. Then insert the string to be inserted into the string. ... .
    Return/Print the new String..

    How do you add special characters to a string in Python?

    To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.