Python String Replace Space with Dash Example

By Hardik Savani October 30, 2023 Category : Python

Hello Developer,

In this tutorial, we will go over the demonstration of python replace space with dash. step by step explain python string replace whitespace with dash. This example will help you how to replace space with dash in python. you'll learn how to replace space with dash in python string.

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

Example

main.py

myString = 'It Solution Stuff com'
  
# python string replace space with dash
newString = myString.replace(' ', '-')
  
print(newString)

Output:

It-Solution-Stuff-com

I hope it can help you...

Tags :
Shares