Ready to level up your Python skills? This guide gives you a rundown of essential built-in functions for strings, numbers, and more. Think of it as your Python cheat sheet!
Each function includes a clear explanation and a practical implementation example. Don't worry about memorizing everything - just know they exist so you can look them up when needed!
String Functions: Master the Text!
Here's a list of powerful tools for working with text in Python. (Remember, characters are just single-letter strings!)
len(string)
Returns the length of a string.
text = "Hello World"
print(len(text)) # Output: 11string.upper()
Converts to uppercase.
text = "hello"
print(text.upper()) # Output: HELLOstring.lower()
Converts to lowercase.
text = "HELLO"
print(text.lower()) # Output: hellostring.strip()
Removes leading/trailing whitespace.
text = " hello "
print(text.strip()) # Output: "hello"string.replace(old, new)
Replaces occurrences of a substring.
text = "Hello World"
print(text.replace("World", "Python")) # Output: Hello Pythonstring.find(substring)
Returns the index of the first occurrence.
text = "Hello World"
print(text.find("o")) # Output: 4string.startswith(prefix)
Checks if the string starts with a prefix.
text = "Hello World"
print(text.startswith("Hello")) # Output: Truestring.split(separator)
Splits the string into a list of substrings.
text = "apple,banana,cherry"
print(text.split(",")) # Output: ['apple', 'banana', 'cherry']string.join(iterable)
Joins elements of an iterable into a string.
words = ["Hello", "World"]
print(" ".join(words)) # Output: "Hello World"ord(character)
Returns the Unicode code point of a character.
print(ord('A')) # Output: 65chr(codepoint)
Returns the character represented by a Unicode code point.
print(chr(65)) # Output: 'A'Math Functions: Number Crunching Power!
Unleash the power of Python for mathematical calculations.
abs(number)
Absolute value.
print(abs(-5)) # Output: 5pow(base, exponent)
Raises a number to a power.
print(pow(2, 3)) # Output: 8round(number, ndigits)
Rounds a number.
print(round(3.14159, 2)) # Output: 3.14max(iterable)
Maximum value in an iterable.
numbers = [1, 5, 3, 9, 2]
print(max(numbers)) # Output: 9min(iterable)
Minimum value in an iterable.
numbers = [1, 5, 3, 9, 2]
print(min(numbers)) # Output: 1sum(iterable)
Sum of elements in an iterable.
numbers = [1, 2, 3, 4, 5]
print(sum(numbers)) # Output: 15divmod(a, b)
Returns quotient and remainder of division.
print(divmod(10, 3)) # Output: (3, 1)bin(x)
Converts an integer to a binary string.
print(bin(10)) # Output: '0b1010'hex(x)
Converts an integer to a hexadecimal string.
print(hex(255)) # Output: '0xff'int(x)
Converts to integer (Implicit Type Conversion).
print(int("42")) # Output: 42
print(int(3.9)) # Output: 3Other Essential Functions
These functions work with various data types and are super useful!
type(object)
Returns the type of an object.
print(type("Hello")) # Output:
print(type(42)) # Output: isinstance(object, classinfo)
Checks if an object is an instance of a class.
print(isinstance(42, int)) # Output: True
print(isinstance("42", int)) # Output: Falserange(start, stop, step)
Generates a sequence of numbers.
for i in range(3):
print(i) # Output: 0, 1, 2enumerate(iterable)
Returns index-value pairs from an iterable.
fruits = ["apple", "banana", "cherry"]
for i, fruit in enumerate(fruits):
print(i, fruit)
# Output: 0 apple, 1 banana, 2 cherryzip(*iterables)
Combines multiple iterables element-wise.
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
# Output: Alice is 25 years old, etc.