Python String Functions Cheat Sheet



Python

Python Classes Cheat Sheet

Whether you’re working with string methods or built-in functions in Python, this Cheat Sheet helps you program the correct order for the operation so you achieve the correct result. Title: Python Strings Cheat Sheet by NouhaThabet - Cheatography.com Created Date: 3103Z.

Field Width and Alignment

List

Python 3 Cheat Sheet

'hey {:10}'.format('hello')

'{:010}'.format(2)

Output: 0000000002 Camtasia download portable.

'{:*^30}'.format('text')

Output: *************text*************

Member and Element Access

String

'{0}, {1}, {2}'.format(1, 2, 3)

Output: 1, 2, 3

'{}, {}, {}'.format(1, 2, 3)

Implicit positional arguments (2.7 and above only)

'{value1}, {value2}, {value2}'.format(value1=1, value2=2, value3=3)

Access keyword arguments by name

'{[1]}'.format(['first', 'second', 'third'])

Access element by index

'{.name}'.format(sys.stdin)

Access element attribute

'{[name]}'.format({'name': 'something'})

Access element by key

Python String Functions Cheat Sheet

Sheet

Use this cheat sheet as a guide in the beginning and come back to it when needed, and you’ll be well on your way to becoming a pro programmer in Python. Join my email list with 1k+ people to get The Complete Python for Data Science Cheat Sheet Booklet for Free. Python recognizes single and double quotes as the same thing, the beginning and ends of the strings. “string list” ‘string list’ ‘string list’ ‘string list’ Now what if you have a quote in the middle of the string? Python needs help to recognize quotes as part of the English language and not as part of the Python language.

Numerical Representation

'{:x}'.format(100)

Output: 64

'{:X}'.format(3487)

Output: D9F

'{:#x}'.format(100)

Output: 0x64

'{:b}'.format(100)

Output: 1100100

'{:c}'.format(100)

Output: d

'{:d}'.format(100)

Output: 100

'{:,}'.format(1000000)

Output: 1,000,000

'{:o}'.format(100)

Output: 144

'{:n}'.format(100)

Like d, but uses locale information for separators

'{:e}'.format(0.0000000001)

Exponent notation

'{:E}'.format(0.0000000001)

Exponent notation (capital 'E')

'{:f}'.format(3/14.0)

Fixed point

'{:g}'.format(3/14.0)

General format

'{:%}'.format(0.66)

Percentage

'{:.3}'.format(0.214286)

Precision

Conversions

'{!r}'.format('string')

Output: 'string'

'{!s}'.format(1.53438987)

Output: 1.53438987