

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Take in a String and Replace Every Blank Space with Hyphen
When it is required to take a string and replace every blank space with a hyphen, the ‘replace’ method can be used. It takes two parameters, the blank space, and the value with which it needs to be replaced (hyphen in this case).
Below is a demonstration of the same −
Example
my_string = input("Enter a string :") print("The string entered by user is :") print(my_string) my_string = my_string.replace(' ','-') print("The modified string:") print(my_string)
Output
Enter a string : A-B-C-D E- A-B-C-D E- The string entered by user is : A-B-C-D E- The modified string: A-B-C-D--E-
Explanation
An input string is asked to be entered by the user.
It is done with the help of the ‘input’ method.
The string entered by the user is displayed on the console.
The ‘replace’ method is used to replace an empty space with a hyphen.
The changed string is then displayed on the console.
- Related Questions & Answers
- Replacing space with a hyphen in C++
- Java regex program to split a string at every space and punctuation.
- Remove new lines from a string and replace with one empty space PHP?
- How to set a string with hyphen and numbers in MySQL varchar?
- C++ Program to find and replace in a string
- Replace Tab with space in SAP ABAP
- Python Program to Replace all Occurrences of ‘a’ with $ in a String
- JavaScript Insert space after every two letters in string?
- How to replace multiple spaces with a single space in C#?
- Hyphen string to camelCase string in JavaScript
- How to replace all occurrences of a string with another string in Python?
- Python Program to replace a word with asterisks in a sentence
- How to concatenate string vectors separated with hyphen in R?
- How to replace multiple spaces in a string using a single space using Java regex?
- How to get a space-padded string with the original string right-justified in Python?
Advertisements