Python Cheat Sheet Part - 2
Sandeep
Posted on May 1, 2022
Strings
Python string is a sequence of characters, and each character can be individually accessed using its index.
String
You can create Strings by enclosing text in both forms of quotes - single quotes or double quotes.
variable_name = "String Data"
Slicing
Slicing refers to obtaining a sub-string from the given string. The following code will include index 1, 2, 3, and 4 for the variable named var_name
var_name[1 : 5]
isalnum() method
Returns True if all the characters in the string are alphanumeric, else False
string_variable.isalnum()
isalpha() method
Returns True if all the characters in the string are alphabets
string_variable.isalpha()
isdecimal() method
Returns True if all the characters in the string are decimals
string_variable.isdecimal()
isdigit() method
Returns True if all the characters in the string are digits
string_variable.isdigit()
islower() method
Returns True if all characters in the string are lower case
string_variable.islower()
isspace() method
Returns True if all characters in the string are whitespaces
string_variable.isspace()
isupper() method
Returns True if all characters in the string are upper case
string_variable.isupper()
lower() method
Converts a string into lower case equivalent
string_variable.lower()
upper() method
Converts a string into upper case equivalent
string_variable.upper()
strip() method
It removes leading and trailing spaces in the string
string_variable.strip()
List
A List in Python represents a list of comma-separated values of any data type between square brackets.
var_name = [element1, element2, ...]
index method
Returns the index of the first element with the specified value
list.index(element)
append method
Adds an element at the end of the list
list.append(element)
extend method
Add the elements of a given list (or any iterable) to the end of the current list
list.extend(iterable)
insert method
Adds an element at the specified position
list.insert(position, element)
pop method
Removes the element at the specified position and returns it
list.pop(position)
remove method
The remove() method removes the first occurrence of a given item from the list
list.remove(element)
clear method
Removes all the elements from the list
list.clear()
count method
Returns the number of elements with the specified value
list.count(value)
reverse method
Reverses the order of the list
list.reverse()
sort method
Sorts the list
list.sort(reverse=True|False)
Tuples
Tuples are represented as comma-separated values of any data type within parentheses.
Tuple Creation
variable_name = (element1, element2, ...)
Lets talk about some of the tuple methods:
count method
It returns the number of times a specified value occurs in a tuple
tuple.count(value)
index method
It searches the tuple for a specified value and returns the position.
tuple.index(value)
Sets
A set is a collection of multiple values which is both unordered and unindexed. It is written in curly brackets.
Set Creation: Way 1
var_name = {element1, element2, ...}
Set Creation: Way 2
var_name = set([element1, element2, ...])
Set Methods
Lets talk about some of the methods of sets:
add() method
Adds an element to a set
set.add(element)
clear() method
Remove all elements from a set
set.clear()
discard() method
Removes the specified item from the set
set.discard(value)
intersection() method
Returns intersection of two or more sets
set.intersection(set1, set2 ... etc)
issubset() method
Checks if a set is a subset of another set
set.issubset(set)
pop() method
Removes an element from the set
set.pop()
remove() method
Removes the specified element from the set
set.remove(item)
union() method
Returns the union of two or more sets
set.union(set1, set2...)
Posted on May 1, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.