Python Program to Take in a String and Replace Every Blank Space with Hyphen
Strings in Python:
A string is one of the most frequent data types in any computer language. A string is a collection of characters that can be used to represent usernames, blog posts, tweets, or any other text content in your code. You can make a string and assign it to a variable by doing something like this.
given_string='btechgeeks'
Strings are considered immutable in Python, once created, they cannot be modified. You may, however, construct new strings from existing strings using a variety of approaches. This form of programming effort is known as string manipulation.
Examples:
Example1:
Input:
given string = hello this is BtechGeeks
Output:
The original string before modification = hello this is BtechGeeks
The new string after modification = hello-this-is-BtechGeeks
Example2:
Input:
given string = files will be upload to a folder you can read those files in the program folder
Output:
Enter some random string = files will be upload to a folder you can read those files in the program folder
The original string before modification = files will be upload to a folder you can read those files in the program folder
The new string after modification = files-will-be-upload-to-a-folder-you-can-read-those-files-in-the-program-folder
Program to Take in a String and Replace Every Blank Space with Hyphen
Below are the ways to scan the string and replace every blank space with a hyphen in Python.
- Using replace function (Static Input)
- Using replace function (User Input)
Method #1:Using replace function (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Using the replace function replace all blank space with a hyphen by providing blank space as the first argument and hyphen as the second argument in replace function.
- Print the modified string.
- The Exit of the Program.
Below is the implementation:
# Give the string as static input and store it in a variable.
given_string = 'hello this is BtechGeeks'
# printing the original string before modification
print('The original string before modification =', given_string)
# Using the replace function replace all blank space with a hyphen by providing blank space as the first argument
# and hyphen as the second argument in replace function.
modified_string = given_string.replace(' ', '-')
# printing the new string after modification
print('The new string after modification =', modified_string)
Output:
The original string before modification = hello this is BtechGeeks
The new string after modification = hello-this-is-BtechGeeks
Method #2:Using replace function (User Input)
Approach:
- Give the string as user input using the int(input()) function and store it in a variable.
- Using the replace function replace all blank space with a hyphen by providing blank space as the first argument and hyphen as the second argument in replace function.
- Print the modified string.
- The Exit of the Program.
Below is the implementation:
# Give the string as user input using int(input()) function and store it in a variable.
given_string = input('Enter some random string = ')
# printing the original string before modification
print('The original string before modification =', given_string)
# Using the replace function replace all blank space with a hyphen by providing blank space as the first argument
# and hyphen as the second argument in replace function.
modified_string = given_string.replace(' ', '-')
# printing the new string after modification
print('The new string after modification =', modified_string)
Output:
Enter some random string = files will be upload to a folder you can read those files in the program folder
The original string before modification = files will be upload to a folder you can read those files in the program folder
The new string after modification = files-will-be-upload-to-a-folder-you-can-read-those-files-in-the-program-folder
The replace() function replaces all instances of ” with ‘-‘.