Python Functions by Nandita Sharma XII-C
Python Functions by Nandita Sharma XII-C
1.)Slicing of a string:
Syntax: stringname[start:end:step]
2.len()
Syntax:len(stringname)
3.)split()
Break the string at the specified delimeter and returns a list of substrings.
max no. of splits is optional and by default = -1 i.e. no limit on no. of splits also by default separator is
whitespace.
4.)count()
Syntax: stringname.count(substring,start,end)
5.)capitalize()
Syntax: stringname.capitalize()
6.) title()
Returns the string with first letter of each word in uppercase while rest in lowercase.
Syntax: stringname.title()
7.)isalpha()
Syntax: stringname.isalpha()
8.)isalnum()
Returns true if all characters are alpha numeric i.e. either ‘a’ to ‘z’ or 0-9.
Syntax: stringname.isalnum()
9.)isdigit()
Returns true only if all characters in the string are digits.
Syntax: stringname.isdigit()
10.)islower()
Syntax: stringname.islower()
11.)isupper()
Syntax: stringname.isupper()
12.)lower()
Syntax: stringname.lower()
13.)upper()
Syntax: stringname.upper()
14.)replace()
15.)find()
Returns the index of the first occurrence of the searched substring if found, else return -1.
Syntax: stringname.find(‘substring’,start,end)
16.)index()
Returns the index of the searched substring if found, else raises and error called value error.
Syntax: stringname(substring,[start],[end])
17.)strip()
18.)istitle()
Returns True if all the substrings of the string have first letter capital, else returns False.
Syntax: stringname.istitle()
19.)isspace()
Returns True if the string consists of only space, else returns False.
Syntax: stringname.isspace()
20.)join()
Returns the string in which the string elements are joined by a string separator.
Syntax: separator.join(string)
21.) partition()
Splits the given string using the separator and returns a tuple with 3 parts:-
b)the separator
Syntax: stringname.partition(‘separator’)
22.)swapcase()
Syntax: stringname.swapcase()
23.)startswith()
Returns True if string starts with the substring sub, else returns False.
Syntax: stringname.startswith(sub)
24.)endswith()
Returns True if the string ends with the substring sub, else returns False.
Syntax: stringname.endswith()
25.)reverse()
Reverses the order of elements in a list by replacing the reversed list’s elements with the original
elements in the existing list.
Syntax: listname.reverse()
26.)sort()
Returns a list having all elements arranged in descending order or ascending order.
27.)pop()
Syntax: listname.pop(index)
Default index=-1
28.)remove()
Removes the first occurrence of the element by passing the value from the function and if not found
raises an exception.
Syntax: listname.remove(element)
29.)del()
30.)clear()
Syntax: listname.clear()
Syntax: listname.count(element)
32.)max()
Syntax: max(listname)
33.)min()
Returns the element with the min. value from the list.
Syntax: min(listname)
34.)sum()
Returns the summation of all numeric elements of the list. If any element is string, it will raise VALUE
ERROR.
Syntax: sum(listname)
35.)append()
Syntax: listname.append(item)
36.)extend()
Syntax: listname.extend(list)
37.)insert()
Syntax: listname.insert(index,element)
38.)sorted()
39.)tuple()
40.)len(dictionary)
41.)get()
Item(value) with the given key Is procured if the key exists, else raise a NameError
Syntax: dictionaryname.get(key,[default])
42.)items()
Returns all of the items in the dictionary as a sequence of (key,value) tuples in no particular order.
Syntax: dictionaryname.items()
43.)keys()
Syntax: dictionaryname.keys()
44.)values()
Syntax: dictionaryname.values()
45.)fromkeys()
A new dictionary is created from a sequence containing all the keys and a common value.
Syntax: dict.fromkeys(keys,value[])
46.)setdefault()
Inserts a new key:value pair only if the key doesn’t already exist but if the key exists then it’s current
value is returned.
Syntax: dictionaryname.setdefault(key,value)
47.)update()
Merges the key:value pairs from the new dictionary into the original dictionary and overrides any items
with the same keys already there.
48.)copy()
49.)pop()
Removes and returns the dictionary element associated to the passed key.
Syntax: dictionaryname.pop(key,value)
50.)popitem()
51.)clear()
Syntax: dictionaryname.clear()
52.)sorted()
Syntax: sorted(dictionaryname,[reverse=False])
53.)max(),min(), sum()
Gives maximum key, minimum key or sum of the key from the dictionary.
Syntax: max(dictionaryname)
min(dictionaryname)
sum(dictionaryname)