- This topic is empty.
-
AuthorPosts
-
June 27, 2026 at 10:34 am #7064
Many beginners learn Python string methods like
split(),join(), andtitle()separately. However, understanding how they work together is an important milestone. In this lesson, we’ll explore how a string can be converted into a list, processed using a list comprehension, and converted back into a string.The Original String
Suppose we have the following string:
text = "my name is dazy"Our goal is to convert it into:
My Name Is DazyMethod 1: Using
title()The simplest solution is:
text = "my name is dazy" print(text.title())Output
My Name Is DazyThe
title()method capitalizes the first letter of every word in the string.Method 2: Breaking the String into a List
A string can be split into individual words using
split().text = "my name is dazy" words = text.split() print(words)Output
['my', 'name', 'is', 'dazy']Notice that the output is not a string anymore.
It is a list.
You can verify this:
print(type(words))Output
<class 'list'>How
split()WorksThink of the process like this:
"my name is dazy" │ ▼ split() │ ▼ ['my', 'name', 'is', 'dazy']Each word becomes an individual element inside a list.
Using a List Comprehension
Since
split()returns a list, we can process every word individually.text = "my name is dazy" capitalized = [word.capitalize() for word in text.split()] print(capitalized)Output
['My', 'Name', 'Is', 'Dazy']The list comprehension performs the following steps:
- Take the first word.
- Capitalize it.
- Store it in a new list.
- Repeat for every remaining word.
Converting the List Back into a String
Lists cannot be printed as normal sentences.
To join them back together:
text = "my name is dazy" capitalized = [word.capitalize() for word in text.split()] result = " ".join(capitalized) print(result)Output
My Name Is DazyThe
" ".join()method inserts one space between each list element.What Does
join()Return?join()returns a string.['My', 'Name', 'Is', 'Dazy'] │ ▼ " ".join() │ ▼ "My Name Is Dazy"A Common Beginner Mistake
Many beginners write:
[text.title() for word in text.split()]This produces:
[ 'My Name Is Dazy', 'My Name Is Dazy', 'My Name Is Dazy', 'My Name Is Dazy' ]Why?
Because the expression inside the list comprehension is:
text.title()Notice that it never uses the loop variable
word.Python simply evaluates the same expression once for every word.
The Correct List Comprehension
Instead, use:
[word.capitalize() for word in text.split()]Now each iteration processes the current word.
Result:
['My', 'Name', 'Is', 'Dazy']Putting Everything Together
text = "my name is dazy" words = text.split() capitalized = [word.capitalize() for word in words] result = " ".join(capitalized) print(result)Output
My Name Is DazyComplete Flow
Original String │ ▼ text = "my name is dazy" │ ▼ split() │ ▼ ['my', 'name', 'is', 'dazy'] │ ▼ List Comprehension │ ▼ ['My', 'Name', 'Is', 'Dazy'] │ ▼ join() │ ▼ "My Name Is Dazy"Key Takeaways
split()converts a string into a list.join()converts a list back into a string.title()capitalizes the first letter of every word in a string.capitalize()capitalizes only the first letter of a single word.- List comprehensions are ideal for transforming each element of a list.
- The expression inside a list comprehension should normally use the loop variable; otherwise, you’ll repeatedly produce the same value.
Understanding this String → List → String workflow is a fundamental Python skill. You’ll use it frequently for cleaning text, processing files, handling CSV data, and preparing user input for real-world applications.
-
AuthorPosts
- You must be logged in to reply to this topic.
