Common String Methods in Python

Python strings come equipped with a variety of powerful methods for text manipulation. In this lesson, we'll explore some of the most frequently used ones:split(), upper(), lower(),replace(), and join(). Mastering these will allow you to process and transform text data with ease.

split()

Splits a string into a list of substrings. If no separator is specified, it splits by any sequence of whitespace characters (spaces, tabs, newlines).

Default separator (whitespace):

sentence = "hello   world\nnew line"
words = sentence.split()
print(words)
['hello', 'world', 'new', 'line']

Custom separator (e.g., comma):

data = "apple,banana,orange"
fruits = data.split(',')
print(fruits)
['apple', 'banana', 'orange']

upper()

Returns a new string with all alphabetic characters converted to uppercase.

greeting = "Hello World!"
loud_greeting = greeting.upper()
print(loud_greeting)
HELLO WORLD!

lower()

Returns a new string with all alphabetic characters converted to lowercase.

mixed_case = "PyThOn Is FuN"
calm_case = mixed_case.lower()
print(calm_case)
python is fun

replace()

Returns a new string where all occurrences of a specified substring are replaced with another substring.

message = "I like cats. Cats are cool."
newMessage = message.replace("cats", "dogs")
print(newMessage)
I like dogs. Dogs are cool.

Note: You can also specify a third argument for the maximum number of replacements.
message.replace("Cats", "dogs", 1)'I like dogs. Cats are cool.'

join()

Concatenates elements of an iterable (like a list of strings) into a single string. The string whose method is called is inserted between each given element.

Joining with a hyphen:

my_list = ['a', 'b', 'c']
joined_string = "-".join(my_list)
print(joined_string)
a-b-c

Joining with a space:

words_list = ['Python', 'is', 'awesome']
sentence = " ".join(words_list)
print(sentence)
Python is awesome

Important: join() is a method of the separator string, not the list.
Correct: "separator".join(list_of_strings)
Incorrect: list_of_strings.join("separator") (This will raise an AttributeError)

Your Turn: Practice Time!

Using the sentence variable from the starter code ("hello world"):

  1. Split the sentence by spaces into a list called words.
  2. Create a new string called shout which is the uppercase version of sentence.
  3. Create a new string called joined by joining the elements in your words list with a hyphen ("-") as the separator.

Modify the starterCode to achieve this. The print statements at the end will help you see your results.

Section 1/6split()

split()

Splits a string into a list of substrings. If no separator is specified, it splits by any sequence of whitespace characters (spaces, tabs, newlines).

Common String Methods in Python

Python strings come equipped with a variety of powerful methods for text manipulation. In this lesson, we'll explore some of the most frequently used ones:split(), upper(), lower(),replace(), and join(). Mastering these will allow you to process and transform text data with ease.

split()

Splits a string into a list of substrings. If no separator is specified, it splits by any sequence of whitespace characters (spaces, tabs, newlines).

Default separator (whitespace):

sentence = "hello   world\nnew line"
words = sentence.split()
print(words)
['hello', 'world', 'new', 'line']

Custom separator (e.g., comma):

data = "apple,banana,orange"
fruits = data.split(',')
print(fruits)
['apple', 'banana', 'orange']

Output:

Click "Check" to run your code.