Dealing with Strings

KELAS DATA SCRAPING




Kampus Remote Worker Indonesia | 2024

Data Scraping | rianaditro

Introduction

When working on data scraping tasks, string manipulation is crucial for cleaning and processing the extracted data.

print("We are learning Python string!")
Data Scraping | rianaditro

Common String Operations

1. f-strings

product = "Laptop"
price = 999.99
text = f"Product: {product}, Price: ${"${price}"}"
print(text)  # Output: "Product: Laptop, Price: $999.99"

2. lower() / upper()

text = "Product Name"
normalized_text = text.lower()  # Convert to lowercase
print(normalized_text)  # Output: "product name"
Data Scraping | rianaditro

3. strip()

text = "  Product Name  "
cleaned_text = text.strip()  # Removes leading and trailing spaces
print(cleaned_text)  # Output: "Product Name"

4. join()

words = ["Product", "Name"]
full_text = " ".join(words)  # Join with space
print(full_text)  # Output: "Product Name"
Data Scraping | rianaditro

5. split()

text = "Price: $20.99"
parts = text.split(": ")  # Split by ": "
print(parts[1])  # Output: "$20.99"

6. replace()

text = "$20.99"
cleaned_text = text.replace("$", "")  # Remove dollar sign
print(cleaned_text)  # Output: "20.99"
Data Scraping | rianaditro

Class Activity

message = "   Hello, world! How, are you today? "
print(message)

Manipulate the current message to get the output hello-world!-how;-are-you-today?

Additional Finish this String Exercise!

Data Scraping | rianaditro

Learn More

Join us on this self-study journey! Click the link below to get started.

Data Scraping | rianaditro

Discover Advanced Topic

Master this advanced topic and supercharge your skills.

Data Scraping | rianaditro

Homework Assignment

"""
    John Doe can be contacted via 
    email at john.doe@example.com or 
    call him at 8000-1234-4567
"""

From this text, extract name, email, phone number in the dictionary.

Optional Try to solve one or more Strings Challenges.

Data Scraping | rianaditro




Thank you

Any Question?




Kampus Remote Worker Indonesia | 2024

Data Scraping | rianaditro

<br>

<br>