str.upper() / .lower()

Python · Strings

Theory

.upper() returns a copy of a string with every letter converted to uppercase, and .lower() returns a copy in lowercase; digits, spaces and symbols stay as they are. With name = 'Asha Rao', name.upper() gives 'ASHA RAO'. Strings in Python are immutable, so these methods never change name itself: they build and return a new string, and if you do not store the result (name = name.upper()) it is simply lost.

Their main job in data work is normalising text so comparisons ignore case. 'Pune' == 'PUNE' is False, but 'Pune'.lower() == 'PUNE'.lower() is True. Related methods: .title() capitalises each word, .capitalize() only the first letter, .swapcase() flips each letter. For caseless matching of international text, .casefold() is stronger than .lower(): 'Straße'.casefold() gives 'strasse'.

Why we use it

Real data is typed by humans: 'pune', 'Pune' and 'PUNE' all mean the same city. Converting to one case before comparing, grouping or deduplicating prevents the same value from being counted as three.

When to use it

Use .lower() (or .casefold()) before comparing or grouping user-entered text, and .upper() for codes like state or country codes. Do not use them on text where case carries meaning, such as passwords or case-sensitive IDs.

How it works

  1. Call name.upper()
  2. Read the original string 'Asha Rao' (it is never modified)
  3. Walk through each character
  4. Is the character a lowercase letter? Yes: Use its uppercase form. No: Copy it unchanged.
  5. Build a new string from the results
  6. Return 'ASHA RAO'; name is still 'Asha Rao'

Syntax

string.upper()
string.lower()

Example

name = 'Asha Rao'
print(name.upper())
print(name.lower())

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic