liveBook · Manning

Uppercase conversion

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

A Python string method that returns a copy of the string with its cased characters converted to uppercase.

upper() is a Python string method that returns an uppercase copy of a string. Call it with parentheses, as in word.upper(); word.upper without parentheses refers to the method itself rather than executing it. section 2.1.9

Convert and test a word

>>> word = 'narwhal' >>> word.upper() 'NARWHAL' >>> word 'narwhal' >>> word.upper().isupper() True The original string remains unchanged, so the returned value can be printed, assigned, or passed to another string method. section 2.1.9

Use upper() when uppercase text helps normalize input or produce output:

  • Check a first character without separately handling uppercase and lowercase input, such as testing whether word[0].upper() belongs to a collection of letters. section 2.1.13
  • Write transformed text to a file with out_fh.write(text.upper()) or print(text.upper(), file=out_fh). section 5.5.4
  • Create uppercase dictionary keys while reading lines with lookup[line[0].upper()] = line.rstrip(). section 7.3.2

Now that we know word is a string (str), we have all these incredibly useful methods we can call on the variable. (A method is a function that belongs to a variable like word.)

For instance, if I wanted to shout about the fact that we have a narwhal, I could print it in UPPERCASE LETTERS. If I search through the help, I will see that there is a function called str.upper(). Here is how you can call or execute that function:

>>> word.upper()
'NARWHAL'

Inspect the interactive example showing upper() called on a string and producing uppercase output. section 2.1.9

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Explains invocation, returned values, non-mutating behavior, chaining, and practical file and dictionary applications.
Examples
Demonstrates narwhal conversion, chained isupper() testing, uppercase file output, and uppercase line-initial dictionary keys.
What it assumes
Assumes basic variables, strings, indexing, methods, collections, loops, and file handling; reinforces method calls and string behavior.
What you can do after
Apply upper() to transform strings, normalize comparisons, chain string operations, and emit uppercase output.
How it connects
Connects string methods with str.isupper(), string indexing, method chaining, file output, and dictionaries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.