Skip to main content
Practice

String Operations

Strings are sequences of characters used to store text like names, messages, or categories. In data analysis, you’ll often work with strings — to clean, combine, or transform them.

Let’s explore some common operations.


Combining Strings

To combine two or more strings, use the + operator:

first_name = "Emily"
last_name = "Clark"
full_name = first_name + " " + last_name
print("Full Name:", full_name)

💡 This joins the first and last names with a space in between.


Changing Case

Sometimes you’ll need to standardize text (e.g., making everything uppercase):

greeting = "hello world"
print(greeting.upper()) # HELLO WORLD
print(greeting.capitalize()) # Hello world

upper() makes all letters capital. capitalize() makes only the first letter capital.


Removing Extra Spaces

Users often type extra spaces accidentally. You can clean them using strip():

raw_input = "   data science   "
clean_input = raw_input.strip()
print("Cleaned:", clean_input)

strip() removes spaces at the start and end — useful for cleaning form inputs.


Replacing Text

If a string contains outdated or incorrect terms, use replace():

message = "I love Java"
updated_message = message.replace("Java", "Python")
print(updated_message)

This finds "Java" in the text and replaces it with "Python".


Why This Matters

You'll work with string data in surveys, file names, timestamps, and user input. Being able to clean and standardize this text is essential for any data analysis project.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.