0% found this document useful (0 votes)
7 views

CheatSheet Python

The document describes various Python operators and their uses, different data types in Python, the print function and its parameters, and string methods in Python.

Uploaded by

imoyaf
Copyright
© © All Rights Reserved
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CheatSheet Python

The document describes various Python operators and their uses, different data types in Python, the print function and its parameters, and string methods in Python.

Uploaded by

imoyaf
Copyright
© © All Rights Reserved
Available Formats
Download as XLSX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Operators Operation Example

** Exponent 2 ** 3 = 8
% Modulus/Remainder 22 % 8 = 6
// Integer division 22 // 8 = 2
/ Division 22 / 8 = 2.75
* Multiplication 3*3=9
- Subtraction 5-2=3
+ Addition 2+2=4
var += 1 Addition var = var + 1
var -= 1 Subtraction var = var - 1
var *= 1 Multiplication var = var * 1
var /= 1 Division var = var / 1
var %= 1 Modulus/Remainder var = var % 1
Data Type Examples
Integers -2, -1, 0, 1, 2, 3, 4, 5
Floating-point numbers -1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25
Strings 'a', 'aa', 'aaa', 'Hello!', '11 cats'
print(*objects, sep=' ', end='\n', file=None, flush=False)
Function 1st argument 2nd argument 3rd argument
print() *objects, sep=' ', end='\n',
The thing to be printed. Can be Separator between End after all objects are
Print objects to the text
basically anything: string, float, objects printed. Default printed. Default 'New
stream file
variable, list, dictionary… 'space' line'

* before object prints its elements


unpacked.

>>> print("car")
car
>>> print(*"car")
car

>>> print(['car', 'bike', 'airplane'])


['car', 'bike', 'airplane']
>>> print(*['car', 'bike', 'airplane'])
car bike airplane

>>> print(range(1,5))
range(1, 5)
>>> print(*range(1,5))
1234
4th argument 5th argument
file= None, flush=False

>>> print('There are', 6, 'members of Monty Python')


There are 6 members of Monty Python

>>> message = 'There are' + 6 + 'members of Monty Python'


Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str

>>> message = 'There are' + str(6) + 'members of Monty


Python'
>>> print(message)
There are6members of Monty Python

>>> print('There are', 6, 'members of Monty Python', sep='😀')


There are😀6😀members of Monty Python

>>> print('There are', 6, 'members of Monty Python', sep=' ')


There are 6 members of Monty Python

>>> print('There are', 6, 'members of Monty Python',


sep=None)
There are 6 members of Monty Python

>>> print('There are', 6, 'members of Monty Python', sep='')


There are6members of Monty Python

>>> print('There are', 6, 'members of Monty Python', sep='\n')


There are
6
members of Monty Python
Method Description Parameters

Converts the first character to upper


capitalize() Takes no parameters
case (and the rest to lower caps)

casefold() Converts all string into lower case Takes no parameters

string.center(length, character)
- length is required.
center() Returns a centered string
- character is optional and default is
" " (space).

string.count(value, start, end)


- value is required, it is the string to
value to search for.
- start is optional, an integer
Returns the number of times a
count() marking the position to start the
specified value occurs in a string
search. Default is 0.
- end is optional. An Integer marking
the position to end the search.
Default is the end of the string

Returns an encoded version of the


encode()
string
Returns true if the string ends with
endswith()
the specified value

expandtabs() Sets the tab size of the string

Searches the string for a specified


find() value and returns the position of
where it was found

format() Formats specified values in a string

format_map(
Formats specified values in a string
)
Searches the string for a specified
index() value and returns the position of
where it was found
Returns True if all characters in the
isalnum()
string are alphanumeric
Returns True if all characters in the
isalpha()
string are in the alphabet
Returns True if all characters in the
isascii()
string are ascii characters
Returns True if all characters in the
isdecimal()
string are decimals
Returns True if all characters in the
isdigit()
string are digits
Returns True if the string is an
isidentifier()
identifier
Returns True if all characters in the
islower()
string are lower case
Returns True if all characters in the
isnumeric()
string are numeric
Returns True if all characters in the
isprintable()
string are printable
Returns True if all characters in the
isspace()
string are whitespaces
Returns True if the string follows the
istitle()
rules of a title
Returns True if all characters in the
isupper()
string are upper case
Converts the elements of an iterable
join()
into a string
Returns a left justified version of the
ljust()
string
lower() Converts a string into lower case
Returns a left trim version of the
lstrip()
string
Returns a translation table to be
maketrans()
used in translations
Returns a tuple where the string is
partition()
parted into three parts
Returns a string where a specified
replace() value is replaced with a specified
value

Searches the string for a specified


rfind() value and returns the last position of
where it was found

Searches the string for a specified


rindex() value and returns the last position of
where it was found

Returns a right justified version of


rjust()
the string
Returns a tuple where the string is
rpartition()
parted into three parts
Splits the string at the specified
rsplit()
separator, and returns a list
Returns a right trim version of the
rstrip()
string
Splits the string at the specified
split()
separator, and returns a list
Splits the string at line breaks and
splitlines()
returns a list
Returns true if the string starts with
startswith()
the specified value
Returns a trimmed version of the
strip()
string

Swaps cases, lower case becomes


swapcase()
upper case and vice versa

Converts the first character of each


title()
word to upper case
translate() Returns a translated string
upper() Converts a string into upper case

Fills the string with a specified


zfill()
number of 0 values at the beginning
Example
>>> sentence = "i love PYTHON"
>>> print(sentence.capitalize())
I love python

>>> text = "pYtHon"


>>> print(text.casefold())
python

>>> s = 'I love Python'


>>> print(s.center(30,"-"))
--------I love Python---------

>>> txt = "I love apples, apple are my favorite fruit"


>>> print(txt.count("apple", 10, 24))
1

You might also like