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: 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 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 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 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 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 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 `DATA_A` is equal to `DATA_B` (ignoring letter case)
# 2. Define variable `result` with the solution
# 3. Run doctests - all must succeed

# %% Polish
# 1. Sprawdź czy `DATA_A` jest równa `DATA_B` (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 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
DATA_A = 'Angus MacGyver III'
DATA_B = 'Angus Macgyver III'

# %% Result
result = ...