String Manipulation & Dictionaries

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

String Manipulation

Summary of String Manipulation Methods and Functions


Function /
S.No. Description
Method
Returns a copy of the string with its first character
1. string.capitalize( )
capitalized.
Returns the lowest index in the string where the
string.find(sub,
2. substring sub is found within the slice range of start
start, end)
and end. Returns -1 if sub is not found.
Returns True if the characters in the string are
3. string.isalnum( ) alphanumeric (alphabets and numbers) and there is
at least one character, False otherwise.
Returns True if all characters in the string are
4. string.isalpha( ) alphabetic and there is at least one character, False
otherwise.
Returns True if all characters in the string are digits.
5. string.isdigit( ) There must be at least one character, otherwise it
returns False.
Returns True if all cased characters in the string are
6. string.islower( ) lowercase. There must be at least one cased
character. It returns False otherwise.
Returns True if there are only whitespace characters
7. string.isspace( ) in the string. There must be at least one character. It
returns False otherwise.
Tests whether all cased characters in the string are
8. string.isupper( ) uppercase and requires that there be at least on cased
character. Returns True if so and False otherwise.
9. string.lower( ) Returns a copy of the string converted to lowercase.

10. string.upper( ) Returns a copy of the string converted to uppercase.


Returns a copy of the string with leading characters
removed.
If used without any argument, it removes the leading
11. string.lstrip([char]) whitespaces.
One can use the optional chars argument to specify a set
of characters to be removed.

Anything you learn is not wasted


The chars arguments is not a prefix ; rather, all

r
combinations of its values (all possible substrings from

i to
the given string argument chars) are stripped when they
lead the string.
Returns a copy of the string with trailing characters

Ed
removed.
If used without any argument, it removes the leading
whitespaces.
12. string.rstrip([char]) The chars argument is a string specifying the set of
characters to be removed.

F
The chars arguments is not a prefix ; rather, all
combinations of its values are stripped.

PD
Dictionaries
Summary of Dictionary Manipulation Methods and Functions
te r
Function / Method
S.No. Description
[ D is Dictionary]
as

1. len(<D>) Returns the number of items in the dictionary.

Returns corresponding value if the key exists, else it


2. <D>.get(key, [default]) returns Python’s Error if default argument is missing
nM

otherwise returns default argument.

3. <D>.items( ) Returns a sequence of (key, value) pairs, the items.

4. <D>.keys( ) Returns a list sequence of keys.


di

5. <D>.values( ) Returns a list sequence of values.


a te

<D>.update(<other- Merges key:value pairs from the new dictionary into the
6.
dictionary>) original dictionary, adding or replacing as needed.

7. <D>.clear( ) Removes all items from the dictionary.


re

Anything you learn is not wasted

You might also like