Remember
title() returns titlecased text using its definition of word boundaries. Apostrophes can create surprising boundaries.
title() returns titlecased text using its definition of word boundaries. Apostrophes can create surprising boundaries.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'hello world'
result = text.title()
print(result)Top-level scope
'hello world'Object 2 · str'Hello World'title() returns titlecased text using its definition of word boundaries. Apostrophes can create surprising boundaries.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'hello world'
result = text.title()
print(result)
|
v
DISPLAYED RESULT
Hello World
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (str)
object 1: 'hello world'
object 2: 'Hello World'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'hello world'
result = text.title()
print(result)Top-level scope
'hello world'Object 2 · str'Hello World'The example has finished. Compare the final output with the values and references above.
Hello World
What do you expect this program to print?
text = 'hello world'
result = text.title()
print(result)title() MethodThe title() method in Python returns a copy of the string where each word starts with an uppercase letter and the remaining letters are lowercase. It’s useful for formatting titles, names, or headings.
string.title()
None – this method does not take any arguments.
A new string with the first letter of each word in uppercase and all other characters in lowercase.
text = "python is fun"
title_text = text.title()
print(title_text)
Python Is Fun
text = "wElcOme to PYTHON world!"
print(text.title())
Welcome To Python World!
If you're building a form or app where users input names, you can use title() to standardize formatting:
name = input("Enter your full name: ")
print("Formatted Name:", name.title())
title() capitalizes letters after any non-letter character, including numbers and punctuation.text = "they're learning python's power!"
print(text.title())
They'Re Learning Python'S Power!
This behavior might not be desirable in every case, so use title() carefully.
Thinking title() changes the original string. It does not. It returns a new string:
text = "hello world"
text.title() # This won't change text
print(text) # Output: hello world
Know the difference between title() and capitalize(). title() capitalizes the first letter of each word, while capitalize() only affects the first letter of the entire string.
title() returns a new string in title caseAsk the user for a book title and print it in proper title case:
book = input("Enter book title: ")
print("Formatted:", book.title())
Connect what you noticed in the example with the idea below.
title() returns titlecased text using its definition of word boundaries. Apostrophes can create surprising boundaries.
title() understands all natural-language word boundaries perfectly.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.