0% found this document useful (0 votes)
3 views4 pages

Python Built-in Methods by Data Type

The document provides a comprehensive list of built-in methods for various data types in Python, including strings, lists, dictionaries, and sets. Each method is accompanied by a brief description and an example of its usage. This serves as a quick reference guide for Python developers to understand and utilize these methods effectively.

Uploaded by

Prashant Patel
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)
3 views4 pages

Python Built-in Methods by Data Type

The document provides a comprehensive list of built-in methods for various data types in Python, including strings, lists, dictionaries, and sets. Each method is accompanied by a brief description and an example of its usage. This serves as a quick reference guide for Python developers to understand and utilize these methods effectively.

Uploaded by

Prashant Patel
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

Here’s a comprehensive list of common built-in methods by data type in Python:

1. String Methods (str)


Method Description Example
.capitalize() Capitalizes first letter "hello".capitalize() # 'Hello'

.casefold()
Case-insensitive string "Hello".casefold() # 'hello'
comparison
.center(width) Centers string in a field "abc".center(5) # ' abc '
.count(sub) Counts occurrences of substring "banana".count('a') # 3
.encode() Encodes string to bytes "abc".encode()

.endswith(suffix)
Checks if string ends with "abc".endswith('c') # True
substring
.expandtabs(tabsize) Expands tabs to spaces "a\tb".expandtabs(4)

.find(sub)
Finds first occurrence or returns "abc".find('b') # 1
-1
.format()
Formats string using "Hello {}".format('world')
placeholders
.format_map(mapping) Like format(), but uses a dict
.index(sub)
Like find(), but raises error if "abc".index('b') # 1
not found
.isalnum()
Checks if all characters are "abc123".isalnum() # True
alphanumeric
.isalpha()
Checks if all characters are "abc".isalpha() # True
alphabetic
.isdigit()
Checks if all characters are "123".isdigit() # True
digits
.islower()
Checks if all characters are "abc".islower() # True
lowercase
.isspace()
Checks if string contains only " \t".isspace() # True
whitespace
.istitle() Checks if string is titlecased "Hello World".istitle() # True

.isupper()
Checks if all characters are "ABC".isupper() # True
uppercase
.join(iterable)
Joins iterable elements into a ",".join(['a','b']) # 'a,b'
string
.ljust(width) Left-justifies string "abc".ljust(5) # 'abc '
.lower() Converts to lowercase "ABC".lower() # 'abc'
.lstrip() Removes leading whitespace " abc".lstrip() # 'abc'
Method Description Example
.partition(sep)
Splits string at first occurrence "a,b,c".partition(',')
of separator
.replace(old, new) Replaces substring "abc".replace('a', 'x') # 'xbc'

.rfind(sub)
Finds last occurrence of "banana".rfind('a') # 5
substring
.rindex(sub)
Like rfind(), but raises error if
not found
.rjust(width) Right-justifies string "abc".rjust(5) # ' abc'

.rpartition(sep)
Splits string at last occurrence of "a,b,c".rpartition(',')
separator
.rsplit(sep=None,
maxsplit=-1) Splits string from right
.rstrip() Removes trailing whitespace "abc ".rstrip() # 'abc'
.split(sep=None, "a,b,c".split(',') #
maxsplit=-1) Splits string by separator ['a','b','c']
.splitlines() Splits string at line breaks
.startswith(prefix)
Checks if string starts with "abc".startswith('a') # True
prefix
.strip()
Removes leading and trailing " abc ".strip() # 'abc'
whitespace
.swapcase() Swaps case of letters "AbC".swapcase() # 'aBc'
"hello world".title() # 'Hello
.title() Converts string to title case World'

.translate(table)
Replaces characters using
translation table
.upper() Converts to uppercase "abc".upper() # 'ABC'
.zfill(width) Pads string with zeros on the left "42".zfill(5) # '00042'

2. List Methods (list)


Method Description Example
[1, 2].append(3) # [1, 2,
.append(x) Adds item to end 3]
.clear() Removes all items [1, 2].clear() # []
.copy() Returns shallow copy
.count(x) Counts occurrences of x [1, 2, 2].count(2) # 2
[1, 2].extend([3,4]) #
.extend(iterable) Extends list by appending elements [1,2,3,4]
.index(x[, start[, end]]) Returns first index of x [1,2,3].index(2) # 1
[1, 3].insert(1, 2) #
.insert(i, x) Inserts x at position i [1,2,3]
Method Description Example
.pop([i])
Removes and returns item at index i [1,2,3].pop() # 3
(default last)
.remove(x) Removes first occurrence of x [1,2,2].remove(2) # [1,2]
.reverse() Reverses list in place
.sort(key=None,
reverse=False) Sorts list in place

3. Dictionary Methods (dict)


Method Description Example
.clear() Removes all items [Link]()
.copy() Returns shallow copy
[Link](['a','b'],
.fromkeys(seq[, value]) Creates dict from keys with given value 0)
.get(key[, default]) Returns value for key if key exists [Link]('key', 0)
.items() Returns view of (key, value) pairs
.keys() Returns view of keys
.pop(key[, default]) Removes and returns value for key
.popitem()
Removes and returns arbitrary (key,
value) pair
.setdefault(key[, Returns value if key exists, else sets
default]) default
.update([other]) Updates dict with another dict or iterable
.values() Returns view of values

4. Set Methods (set)


Method Description Example
.add(elem) Adds element [Link](1)
.clear() Removes all elements
.copy() Returns shallow copy
.difference(*others) Returns difference of sets
.difference_update(*others) Removes elements found in others
.discard(elem) Removes element if present
.intersection(*others) Returns intersection of sets
.intersection_update(*others) Updates set with intersection
.isdisjoint(other) Checks if sets have no elements in common
.issubset(other) Checks if set is subset of another
Method Description Example
.issuperset(other) Checks if set is superset of another
.pop() Removes and returns an arbitrary element
.remove(elem) Removes element, raises error if not present
.symmetric_difference(other) Returns elements in either set but not both
.symmetric_difference_update(other) Updates with symmetric difference
.union(*others) Returns union of sets
.update(*others) Updates set with union of others

You might also like