• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Using Python to Append Character to String Variable

Using Python to Append Character to String Variable

July 26, 2022 Leave a Comment

To append a character to a string in Python, the easiest way is with the + operator.

string = "this is a string"
character = "."

string_appended = string + character

print(string_appended)

#Output:
this is a string.

When working with strings in Python, the ability to easily be able to modify the values of these variables is valuable.

One such case is if you want to append characters to a string variable and add characters at the end of the string.

To append a character to a string in Python, the easiest way is with the + operator. + concatenates two strings together and therefore we can use it to append a character to a string.

Below is an example showing you how to append a character to a string in Python with +.

string = "this is a string"
character = "."

string_appended = string + character

print(string_appended)

#Output:
this is a string.

How to Prepend Character to String in Python

If you want to add a character at the beginning of a string, instead of the end of a string, you can modify the example from above.

To prepend a character to a string variable, you can use + and switch the order from above.

Below is an example showing you how to prepend a character to a string in Python with +.

string = "this is a string"
character = "."

string_prepended = character + string

print(string_prepended)

#Output:
.this is a string

Hopefully this article has been useful for you to learn how to append a character to a string variable in Python.

Other Articles You'll Also Like:

  • 1.  How to Add Commas to Numbers in Python
  • 2.  Using Python to Flatten Array of Arrays
  • 3.  pandas ewm – Calculate Exponentially Weighted Statistics in DataFrame
  • 4.  Remove Trailing Zeros from String with rstrip() in Python
  • 5.  Get Random Value from Dictionary in Python
  • 6.  Using Python to Check If Variable is Not None
  • 7.  How to Check if String Contains Uppercase Letters in Python
  • 8.  Using Python to Add String to List
  • 9.  Run Something Every 5 Seconds in Python
  • 10.  Convert String to Boolean Value in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Copyright © 2023 · The Programming Expert · About · Privacy Policy