URLify a Given String in Python - Replace Spaces by %20

URLify a given string

Problem: Write a python program to replace all the spaces in a given string with %20. The length of the string is given and the string may have extra space at the end or front.

Example

Input: "Mr John Wick   ", 12

Output: Mr%20John%20Wick

Try to solve this problem using the inplace algorithm.

Solution

There are two methods to solve this problem.

Method 1: Create a string and copy all the characters from the given string one by one. Whenever a space comes put %20 at that place.

Method 2: First, count the number of spaces(without the leading and trailing space) in the given string. Now find the new length of the given string by this formula: (string_length+total_spaces*2) and insert %20 in place of each space in the reverse order from the end.

The first method will take more space and time than the second one. That's why we'll use method #2 here.

Code


"""URLify a given string"""

def replaceSpaces(string, string_len):
total_space = 0

# Remove all the spaces from the
# leading and trailing position
string = string.strip()
# Count the number of spaces
total_space = string.count(' ')
# Convert the string to a list
string = list(string)
index = string_len + total_space * 2

for f in range(string_len - 2, index - 2):
string.append('0')

for j in range(string_len - 1, 0, -1):
if string[j] == ' ':
string[index - 1] = '0'
string[index - 2] = '2'
string[index - 3] = '%'
index = index - 3
else:
# Item assignment
string[index - 1] = string[j]
index -= 1

return ''.join(string)

# Driver Code
if __name__ == "__main__":
string = "Mr John Wick "
string_len = 12
print(replaceSpaces(string, string_len))

Output

Mr%20John%20Wick

Q&A

index = string_len + total_space * 2

Why was the total_space multiplied by 2?

Answer: In the given string there are two spaces. We have to replace these two spaces with '%20'. Imagine, two spaces are filled up by two '%' characters, then where we will put the remaining '20'?

That's why we are multiplying total_space by 2. One space is for '2' and another space is for '0'.


name = list(name)

Why was a string turned into a list?

Answer: Because a object does not support item assignment.


for f in range(string_len - 2, index - 2):

    string.append('0')

Why we are appending '0' ?

Answer: To occupy more space according to the new length of the given string so that '%20' can be fit perfectly in place of all the spaces.

Exercises: Do it yourself

Problem 1: Solve the same problem. In this case, replace all the space by $1500.

Problem 2: Solve the same problem with this input data. The string length is 13.

Input: 

name = "   I Love Python   "

Summary

In this tutorial, you learned how to replace all spaces in a given string by '%20' using a python program. I hope you've grabbed the logic of this program. If you have any doubts, do comment below. I will reply to you soon.

Have a nice day ahead, cheers!

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post