0% found this document useful (0 votes)
24 views

Python Functions by Nandita Sharma XII-C

The document describes various string, list, and dictionary methods in Python. It outlines over 50 functions and their syntax, including methods for slicing, length, splitting, counting, capitalization, checking characters, finding indices, stripping, joining, sorting, removing elements, retrieving values, copying, and more.

Uploaded by

Nandita Sharma
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)
24 views

Python Functions by Nandita Sharma XII-C

The document describes various string, list, and dictionary methods in Python. It outlines over 50 functions and their syntax, including methods for slicing, length, splitting, counting, capitalization, checking characters, finding indices, stripping, joining, sorting, removing elements, retrieving values, copying, and more.

Uploaded by

Nandita Sharma
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/ 7

PYTHON FUNCTIONS

1.)Slicing of a string:

Syntax: stringname[start:end:step]

Default start=0 and step=1 the end value isn’t included.

2.len()

The nom of elements/characters in the string is returned as an integer value.

Syntax:len(stringname)

3.)split()

Break the string at the specified delimeter and returns a list of substrings.

Syntax: stringname.split(separator,[max no. of splits])

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()

To count the no. of substrings in a string.

Syntax: stringname.count(substring,start,end)

Start and end are optional.

5.)capitalize()

To return the exact copy of string with first letter in capital.

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()

Returns true if string has only alphabets else returns false.

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()

Returns true when all characters in the string are in lowercase.

Syntax: stringname.islower()

11.)isupper()

Returns true when all characters in the string are in uppercase.

Syntax: stringname.isupper()

12.)lower()

Returns a string converted to lowercase.

Syntax: stringname.lower()

13.)upper()

Returns a string converted to uppercase.

Syntax: stringname.upper()

14.)replace()

Replaces all occurrences of old substring with the new substring.

Syntax:stringname.replace(‘old string’,New string')

15.)find()

Returns the index of the first occurrence of the searched substring if found, else return -1.

Syntax: stringname.find(‘substring’,start,end)

Start and end are optional.

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()

Returns the string after removing spaces.

a)from the left- lstrip()

b)from the right- strip()

c)from the both sides- strip()


Syntax: string.strip() or string.strip(chr): removes the character from both the sides.

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)

Where the separator is of string type.

21.) partition()

Splits the given string using the separator and returns a tuple with 3 parts:-

a)substring before the separator

b)the separator

c) substring after the separator

Syntax: stringname.partition(‘separator’)

22.)swapcase()

Returns a string swapping the cases.

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.

Syntax: listname.sort() for ascending

listname.sort(reverse=True) for descending

27.)pop()

Removes an element from the list by passing the index of an element.

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()

Used to remove the element from the list.

del listname[x]:xth element is deleted

del listname: deletes the whole list

del listname[a:b]:deletes elements from a to bth place.

30.)clear()

Removes all elements of a list and returns an empty list.

Syntax: listname.clear()

31.)count() for list

Returns the count of element passed as a parameter.

Syntax: listname.count(element)

32.)max()

Returns the element with the max. value of the list.

Numeric: Mathematical comparison

String: ASCII value comparison

Syntax: max(listname)
33.)min()

Returns the element with the min. value from the list.

Between strings, comparison is each index wise.

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()

Adds an item to the end of the existing list.

Syntax: listname.append(item)

36.)extend()

Adds multiple elements in the form of a list to the existing list.

Syntax: listname.extend(list)

37.)insert()

Inserts an item at a given position.

Syntax: listname.insert(index,element)

38.)sorted()

Returns a new sorted list with sorted elements in it.

Syntax: sorted(listname, reverse=True) for descending order

And reverse=false or not specified for ascending order.

39.)tuple()

Creates tuples from different types of values.

Creating an empty tuple:tuple()

Creating tuple from a string:tuple(string)

40.)len(dictionary)

Returns the count of key-value pairs.

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()

Returns all the keys in the dictionary as a list.

Syntax: dictionaryname.keys()

44.)values()

Returns a list of all the values in the dictionary.

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.

Syntax: dictionaryname.update(other dictionary)

48.)copy()

A copy of the dictionary is made.

Syntax: dictionary name.copy()

49.)pop()

Removes and returns the dictionary element associated to the passed key.

Syntax: dictionaryname.pop(key,value)

50.)popitem()

Removes and returns a (key,value) pair from the dictionary.


Syntax: dictionaryname.popitem()

51.)clear()

Removes all elements from the dictionary.

Syntax: dictionaryname.clear()

52.)sorted()

Returns a sorted list of the dictionary keys.

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)

You might also like