Affiliate link

Python - Strings Formatting

Python provides these built-in functions that you can use on strings for formatting.

pystring = 'a Python string.'

String - Case

FunctionDescriptionExampleResult
capitalizeCapitalize first characterpystring.capitalize()A Python string.
titleCapitalize all the wordspystring.title()A Python String.
upperConvert string to uppercasepystring.upper()A PYTHON STRING.
lowerConvert string to lowercasepystring.lower()a python string.
swapcaseSwap uppercase to lowercase and lowercase to uppercase.pystring.swapcase()A pYTHON STRING.

String - Alignment

FunctionDescriptionExampleResult
centerCenter the string within the number of space definedpystring.center(30)"       a Python string.       "
ljustLeft justify with the number of spacepystring.ljust(30)"a Python string.              "
rjustRight justify with the number of spacepystring.rjust(30)"              a Python string."
Affiliate link
Affiliate link
Advertisment
StudioPress
Fiverr
Get Surfshark VPN
rocket.net