search

Home  >  Q&A  >  body text

Python string case conversion problem

str.lower() The string is all lowercase. str.upper() The string is all uppercase.

>>> str = 'my world, MY PYTHON'
>>> str.lower()
'my world, my python'
>>> str.upper()
'MY WORLD, MY PYTHON'

How can I make the stringcapitalize the first letter of each word? Make str = 'My World, My Python'

天蓬老师天蓬老师2732 days ago1271

reply all(2)I'll reply

  • 仅有的幸福

    仅有的幸福2017-06-28 09:25:09

    Reference article: Issues related to Python string operations

    String case conversion

    str.lower() Strings are all lowercase.
    str.upper() The string is all uppercase.
    str.capitalize() Capitalize the first letter of the string.
    str.title() The first letter of each word in the string is capitalized.

    >>> str = 'my world, MY PYTHON'
    >>> str.lower()
    'my world, my python'
    >>> str.upper()
    'MY WORLD, MY PYTHON'
    >>> str.capitalize()
    'My world, my python'
    >>> str.title()
    'My World, My Python'

    reply
    0
  • 天蓬老师

    天蓬老师2017-06-28 09:25:09

    str.title()

    reply
    0
  • Cancelreply