6.4. String Methodsï
str.upper()- all letters will be uppercasestr.lower()- all letters will be lowercasestr.capitalize()- will uppercase first letter of text, lowercase othersstr.title()- will uppercase first letter of each word, lowercase othersstr.swapcase()- make lowercase letters upper, and uppercase lowerstr.casefold()- Return a version of the string suitable for caseless comparisons
6.4.1. Upperï
str.upper()- all letters will be uppercase
>>> name = 'Angus MacGyver III'
>>>
>>> name.upper()
'ANGUS MACGYVER III'
6.4.2. Lowerï
str.lower()- all letters will be lowercase
>>> name = 'Angus MacGyver III'
>>>
>>> name.lower()
'angus macgyver iii'
6.4.3. Capitalizeï
str.capitalize()- will uppercase first letter of text, lowercase others
>>> name = 'Angus MacGyver III'
>>>
>>> name.title()
'Angus Macgyver Iii'
6.4.4. Titleï
str.title()- will uppercase first letter of each word, lowercase others
>>> name = 'Angus MacGyver III'
>>>
>>> name.capitalize()
'Angus macgyver iii'
6.4.5. Swapcaseï
str.swapcase()- make lowercase letters upper, and uppercase lower
>>> name = 'Angus MacGyver III'
>>>
>>> name.swapcase()
'aNGUS mACgYVER iii'
6.4.6. Casefoldï
str.casefold()- Return a version of the string suitable for caseless comparisons
str.casefold() is a more aggressive method for converting strings to lowercase
than str.lower(). It is designed for caseless matching, especially for Unicode
text, and can handle more language-specific cases (e.g., German "Ã" becomes "ss"
with str.casefold(), but remains "Ã" with str.lower()). Use str.casefold()
when you need reliable, language-independent case-insensitive comparisons. For most
simple lowercase conversions, str.lower() is sufficient.
>>> a = 'ALICE'
>>> b = 'alice'
>>>
>>>
>>> a == b
False
>>>
>>> a.casefold() == b.casefold()
True
6.4.7. Use Case - 1ï
Replace substring:
>>> name = 'Angus MacGyver Iii'
>>> name.replace('Iii', 'III')
'Angus MacGyver III'
Replace is case sensitive:
>>> name = 'Angus MacGyver Iii'
>>> name.replace('iii', 'III')
'Angus MacGyver Iii'
6.4.8. Assignmentsï
# %% About
# - Name: Type Str Case Lower
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Convert string `DATA` to lowercase letters
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. ZamieÅ ciÄ
g znaków `DATA` na maÅe litery
# 2. Zdefiniuj zmiennÄ
`result` z rozwiÄ
zaniem
# 3. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result
# 'angus macgyver iii'
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is str, \
'Variable `result` has an invalid type; expected: `str`.'
>>> result
'angus macgyver iii'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: str
# %% Data
DATA = 'Angus MacGyver III'
# %% Result
result = ...
# %% About
# - Name: Type Str Case Upper
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Convert string `DATA` to uppercase letters
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. ZamieÅ ciÄ
g znaków `DATA` na duże litery
# 2. Zdefiniuj zmiennÄ
`result` z rozwiÄ
zaniem
# 3. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result
# 'ANGUS MACGYVER III'
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is str, \
'Variable `result` has an invalid type; expected: `str`.'
>>> result
'ANGUS MACGYVER III'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: str
# %% Data
DATA = 'Angus MacGyver III'
# %% Result
result = ...
# %% About
# - Name: Type Str Case Capitalize
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Convert string `DATA` to capitalized form
# (first letter of a string uppercase, all the others letters are lowercase)
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. ZamieÅ ciÄ
g znaków `DATA` na formÄ skapitalizowanÄ
(ang. capitalize)
# (wielka litera na poczÄ
tku ciÄ
gu znaków, pozostaÅe litery maÅe)
# 2. Zdefiniuj zmiennÄ
`result` z rozwiÄ
zaniem
# 3. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result
# 'Angus macgyver iii'
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is str, \
'Variable `result` has an invalid type; expected: `str`.'
>>> result
'Angus macgyver iii'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: str
# %% Data
DATA = 'Angus MacGyver III'
# %% Result
result = ...
# %% About
# - Name: Type Str Case Title
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Convert string `DATA` to title form
# (first letter of each word uppercase and the rest lowercase)
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. ZamieÅ ciÄ
g znaków `DATA` na formÄ tytuÅowÄ
(ang. title)
# (wielka litera na poczÄ
tku każdego sÅowa, pozostaÅe litery maÅe)
# 2. Zdefiniuj zmiennÄ
`result` z rozwiÄ
zaniem
# 3. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result
# 'Angus Macgyver Iii'
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is str, \
'Variable `result` has an invalid type; expected: `str`.'
>>> result
'Angus Macgyver Iii'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: str
# %% Data
DATA = 'Angus MacGyver III'
# %% Result
result = ...
# %% About
# - Name: Type Str Case Swap
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Convert string `DATA` to swapped form
# (change lowercase letters to uppercase, and uppercase letters to lowercase)
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. ZamieÅ ciÄ
g znaków `DATA` na formÄ odwróconÄ
(ang. swap)
# (zamieÅ maÅe litery na wielkie, i wielkie litery na maÅe)
# 2. Zdefiniuj zmiennÄ
`result` z rozwiÄ
zaniem
# 3. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result
# 'aNGUS mACgYVER iii'
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is str, \
'Variable `result` has an invalid type; expected: `str`.'
>>> result
'aNGUS mACgYVER iii'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: str
# %% Data
DATA = 'Angus MacGyver III'
# %% Result
result = ...
# %% About
# - Name: Type Str Case Fold
# - Difficulty: easy
# - Lines: 1
# - Minutes: 1
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Check if `DATA1` is equal to `DATA2` (ignoring letter case)
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed
# %% Polish
# 1. Sprawdź czy `DATA1` jest równa `DATA2` (ignorujÄ
c wielkoÅÄ liter)
# 2. Zdefiniuj zmiennÄ
`result` z rozwiÄ
zaniem
# 3. Uruchom doctesty - wszystkie muszÄ
siÄ powieÅÄ
# %% Expected
# >>> result
# True
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'
>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'
>>> assert type(result) is bool, \
'Variable `result` has an invalid type; expected: `bool`.'
>>> result
True
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
result: str
# %% Data
DATA1 = 'Angus MacGyver III'
DATA2 = 'Angus Macgyver III'
# %% Result
result = ...