5.4. String Methods

  • str.upper() - all letters will be uppercase

  • str.lower() - all letters will be lowercase

  • str.capitalize() - will uppercase first letter of text, lowercase others

  • str.title() - will uppercase first letter of each word, lowercase others

  • str.swapcase() - make lowercase letters upper, and uppercase lower

  • str.casefold() - Return a version of the string suitable for caseless comparisons

5.4.1. Upper

  • str.upper() - all letters will be uppercase

>>> name = 'Angus MacGyver III'
>>>
>>> name.upper()
'ANGUS MACGYVER III'

5.4.2. Lower

  • str.lower() - all letters will be lowercase

>>> name = 'Angus MacGyver III'
>>>
>>> name.lower()
'angus macgyver iii'

5.4.3. Capitalize

  • str.capitalize() - will uppercase first letter of text, lowercase others

>>> name = 'Angus MacGyver III'
>>>
>>> name.title()
'Angus Macgyver Iii'

5.4.4. Title

  • str.title() - will uppercase first letter of each word, lowercase others

>>> name = 'Angus MacGyver III'
>>>
>>> name.capitalize()
'Angus macgyver iii'

5.4.5. Swapcase

  • str.swapcase() - make lowercase letters upper, and uppercase lower

>>> name = 'Angus MacGyver III'
>>>
>>> name.swapcase()
'aNGUS mACgYVER iii'

5.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

5.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'

5.4.8. Assignments

# %% About
# - Name: Type Str Case Lower
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% 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. Define variable `result` with `DATA`
#    in lowercase letters
#    example: 'hello world'
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z `DATA`
#    zapisaną małymi literami
#    przykład: 'hello world'
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# >>> result
# 'angus macgyver iii'

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is str, \
'Variable `result` has invalid type, should be 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: 2

# %% 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. Define variable `result` with `DATA`
#    in uppercase letters
#    example: 'HELLO WORLD'
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z `DATA`
#    zapisaną dużymi literami
#    przykład: 'HELLO WORLD'
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# >>> result
# 'ANGUS MACGYVER III'

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is str, \
'Variable `result` has invalid type, should be 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: 2

# %% 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. Define variable `result` with `DATA`
#    with the first letter capitalized and the rest lowercase
#    example: 'Hello world'
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z `DATA`
#    zapisaną wielką literą na początku i resztą małymi literami
#    przykład: 'Hello world'
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# >>> result
# 'Angus macgyver iii'

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is str, \
'Variable `result` has invalid type, should be 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: 2

# %% 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. Define variable `result` with `DATA`
#    with first letter of each word capitalized and the rest lowercase
#    example: 'Hello World'
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z `DATA`
#    zapisaną wielką literą na początku każdego słowa i resztą małymi literami
#    przykład: 'Hello World'
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# >>> result
# 'Angus macgyver iii'

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is str, \
'Variable `result` has invalid type, should be 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: 2

# %% 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. Define variable `result` with `DATA` with
#    letter case swapped (lowercase to uppercase and uppercase to lowercase)
#    example: 'Hello World'
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z `DATA` zapisaną
#    z odwróconą wielkością liter (małe na duże i duże na małe)
#    przykład: 'Hello World'
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# >>> result
# 'aNGUS mACgYVER iii'

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is str, \
'Variable `result` has invalid type, should be 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: 2

# %% 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. Define variable `result` with `DATA` with
#    result of checking if `DATA_A` is equal to `DATA_B` with case ignored
#    example: 'Hello World' and 'hello world' are equal
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z `DATA` zapisaną
#
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Example
# >>> result
# 'Angus Macgyver Iii'

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> assert type(result) is str, \
'Variable `result` has invalid type, should be 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 = ...