Fundamentals 4 min read

Common Python String Operations: Concatenation, Splitting, Searching, Replacement, and More

This article demonstrates ten essential Python string manipulation techniques—including concatenation, splitting, searching, replacement, case conversion, slicing, formatting, whitespace trimming, list conversion, and URL encoding/decoding—each illustrated with clear code examples and expected output for practical API development.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Common Python String Operations: Concatenation, Splitting, Searching, Replacement, and More

1. String Concatenation Scenario: Build a complete URL for a request.

base_url = "https://api.example.com"
endpoint = "/data"
full_url = base_url + endpoint
print(full_url)

Output: https://api.example.com/data 2. String Splitting Scenario: Extract a specific field from a JSON response.

json_response = '{"id": 1, "name": "John Doe", "email": "[email protected]"}'
response_dict = eval(json_response)  # convert to dict
email = response_dict['email']
print(email)

Output: [email protected] 3. String Searching Scenario: Verify that a response contains a particular text.

response_text = "Welcome to the API documentation."
assert "documentation" in response_text

Output: No output indicates the assertion passed.

4. String Replacement Scenario: Replace placeholders in request parameters.

template = "https://api.example.com/data?user={user_id}"
user_id = "12345"
url = template.format(user_id=user_id)
print(url)

Output: https://api.example.com/data?user=12345 5. String Case Conversion Scenario: Convert request headers to a uniform uppercase form.

headers = {
    "content-type": "application/json",
    "accept": "application/json"
}
upper_headers = {k.upper(): v.upper() for k, v in headers.items()}
print(upper_headers)

Output:

{'CONTENT-TYPE': 'APPLICATION/JSON', 'ACCEPT': 'APPLICATION/JSON'}

6. Substring Extraction Scenario: Extract the path part from a URL.

url = "https://api.example.com/data/12345"
path = url.split("/")[-1]
print(path)

Output: 12345 7. String Formatting Scenario: Use an f‑string to format a message.

name = "John Doe"
message = f"Welcome, {name}!"
print(message)

Output: Welcome, John Doe! 8. Whitespace Trimming Scenario: Clean extra spaces from request parameters.

param = "  user_id=12345  "
cleaned_param = param.strip()
print(cleaned_param)

Output: user_id=12345 9. Splitting to List Scenario: Convert a query‑string into a list of key‑value pairs.

params = "id=12345&name=John Doe"
param_list = params.split("&")
print(param_list)

Output: ['id=12345', 'name=John Doe'] 10. URL Encoding and Decoding Scenario: Encode request parameters for safe transmission.

import urllib.parse
param = "name=John Doe"
encoded_param = urllib.parse.quote(param)
decoded_param = urllib.parse.unquote(encoded_param)
print(encoded_param)
print(decoded_param)

Output: name%3DJohn+Doe and

name=John Doe
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonBackend Developmentprogramming fundamentalsString Manipulation
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.