0% found this document useful (0 votes)
1 views3 pages

Python Beginner Methods Cheatsheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Python Beginner Methods Cheatsheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Beginner Methods & Functions Cheat Sheet

1. STRING METHODS (str)


Strings are text inside quotes: "hello" or 'hello'.

.lower() – Makes all letters lowercase.


text = "HeLLo"
print(text.lower()) # "hello"

.upper() – Makes all letters uppercase.


text = "hello"
print(text.upper()) # "HELLO"

.title() – Capitalizes the first letter of each word.


text = "hello world"
print(text.title()) # "Hello World"

.strip() – Removes spaces or chosen characters from both ends.


text = " hello "
print(text.strip()) # "hello"

.replace(old, new) – Replaces all occurrences of old with new.


text = "I like cats"
print(text.replace("cats", "dogs")) # "I like dogs"

.split(separator) – Breaks the string into a list using separator.


text = "a,b,c"
print(text.split(",")) # ['a', 'b', 'c']

.join(list) – Joins list elements into a string.


words = ["I", "love", "Python"]
print(" ".join(words)) # "I love Python"

.find(substring) – Finds the first position of substring (-1 if not found).


text = "banana"
print(text.find("na")) # 2

.count(substring) – Counts how many times a substring appears.


text = "banana"
print(text.count("a")) # 3

.startswith(prefix) – Checks if a string starts with prefix.


text = "hello"
print(text.startswith("he")) # True

.endswith(suffix) – Checks if a string ends with suffix.


text = "hello"
print(text.endswith("lo")) # True

2. LIST METHODS (list)


Lists are collections of items: [1, 2, 3]

.append(item) – Adds an item to the end.


nums = [1, 2]
nums.append(3)

.insert(index, item) – Inserts an item at position.


nums = [1, 3]
nums.insert(1, 2)

.remove(item) – Removes the first occurrence.


nums = [1, 2, 3, 2]
nums.remove(2)

.pop(index) – Removes item at index and returns it.


nums = [1, 2, 3]
nums.pop()

.sort() – Sorts list ascending.


nums = [3, 1, 2]
nums.sort()

.reverse() – Reverses order of list.


nums = [1, 2, 3]
nums.reverse()

.count(item) – Counts how many times item appears.


nums = [1, 2, 2]
nums.count(2)

.index(item) – Finds index of first occurrence.


nums = [1, 2, 3]
nums.index(2)

.extend(list2) – Adds all from another list.


nums = [1, 2]
nums.extend([3, 4])

3. NUMBER FUNCTIONS

abs(x) – Absolute value.


abs(-5) # 5

round(x, n) – Round to n decimals.


round(3.14159, 2) # 3.14

max(a, b, c) – Largest value.


max(1, 5, 3) # 5

min(a, b, c) – Smallest value.


min(1, 5, 3) # 1

sum(list) – Sum of numbers.


sum([1, 2, 3]) # 6

4. GENERAL BUILT-IN FUNCTIONS


len(x) – Number of items.
len("hello") # 5

type(x) – Data type.


type(42) #

str(x) – Convert to string.


str(123) # "123"

int(x) – Convert to integer.


int("42") # 42

float(x) – Convert to float.


float("3.14") # 3.14

You might also like