How to Replace Space with Dash in Python String?

27-Mar-2023

.

Admin

How to Replace Space with Dash in Python String?

Hi Dev,

In this tutorial, I will show you how to replace space with a dash in a python string. if you have a question about python replacing space with a dash then I will give a simple example with a solution. This article will give you a simple example of a python string replacing whitespace with a dash. This article will give you a simple example of how to replace space with the dash in python. Follow the below tutorial step on how to replace space with a dash in a python string.

In this example, I will add myString variable with the hello string. Then we will use replace() function to replace space with a dash in the python string. So, without further ado, let's see simple examples: You can use these examples with the python3 (Python 3) version.

Example


main.py

myString = 'nicesnippets-com'

# python string replace space with dash

newString = myString.replace(' ', '-')

print(newString)

Output:

nicesnippets-com

#Python