#P0004. 替换 URL 中的内容
替换 URL 中的内容
1. Problem Description
URL (Uniform Resource Locator) is the address that identifies resources on the internet, helping us find webpages, images, files, etc. on the web.
A URL is typically composed of several parts, including:
- Protocol: Common ones are http or https
- Hostname: For example, www.google.com, skyline-ai.space
- Path: Separated by /, helping us locate the resource on the host
- Query Parameters: Used to pass additional information to the server, represented by the part after the question mark
?in the URL
Given a URL, the path contains the word "book". Let's replace all occurrences of "book" with "wiki".
Here are some string operations that might be useful in the problem:
| Operation | Description |
|---|---|
str.find(sub) |
Finds the first occurrence of the substring sub in str, returns -1 if not found |
str.replace(old, new) |
Returns a new string with all occurrences of old replaced by new in str |
str.upper() |
Converts all letters in str to uppercase |
str.lower() |
Converts all letters in str to lowercase |
str.strip(chars) |
Returns a new string with leading and trailing characters in chars removed from str |
str.startswith(sub) |
Checks if str starts with the substring sub |
str.endswith(sub) |
Checks if str ends with the substring sub |
2. Input and Output
Input Method
- A string representing the URL
- Use
input()to read in the string
Output Method
- A string representing the modified URL
- Use
print()to output the result
Input and Output Example
# Input:
https://www.amazon.com/Sweet-Tooth-Desserts-Save-Baking/book/0593581997/ref=zg_d_sccl_2/139-7012919-4701521?pd_rd_w=yHnET
# Output:
https://www.amazon.com/Sweet-Tooth-Desserts-Save-Baking/wiki/0593581997/ref=zg_d_sccl_2/139-7012919-4701521?pd_rd_w=yHnET