String Operators
Assume string variable a holds 'Hello' and variable b holds
‘ComputerScience' , then−
Operator Description Example
+ Concatenation-Adds values on a + b will give
either side of the operator HelloComputerScience
* Repetition – Creates new strings, a*2 will give -HelloHello
concatenating multiple copies of
the same string
[] Slice-Gives the character from a[1] will give e
the given index
[ :] Range Slice – Gives the a[1:4] will give ell
characters from the given range
in Membership- Returns True if a H in a will be true
character exists in the given
string
not in Membership - Returnstrue if a M not inawill givetrue
character does not exist in the
givenstring
String Functions
Method Description
capitalize() Converts the first character to upper case
casefold() Converts string into lower case
center() Returns a centered string
count() Returns the number of times a specified value occurs in a
string
encode() Returns an encoded version of the string
endswith() Returns true if the string ends with the specified value
expandtabs() Sets the tab size of the string
find() Searches the string for a specified value and returns the
position of where it was found
format() Formats specified values in a string
format_map() Formats specified values in a string
index() Searches the string for a specified value and returns the
position of where it was found
isalnum() Returns True if all characters in the string are
alphanumeric
isalpha() Returns True if all characters in the string are in the
alphabet
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
isidentifier() Returns True if the string is an identifier
islower() Returns True if all characters in the string are lower case
isnumeric() Returns True if all characters in the string are numeric
isprintable() Returns True if all characters in the string are printable
isspace() Returns True if all characters in the string are
whitespaces
istitle() Returns True if the string follows the rules of a title
isupper() Returns True if all characters in the string are upper case
join() Joins the elements of an iterable to the end of the string
ljust() Returns a left justified version of the string
lower() Converts a string into lower case
lstrip() Returns a left trim version of the string
maketrans() Returns a translation table to be used in translations
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with
a specified value
rfind() Searches the string for a specified value and returns the
last position of where it was found
rindex() Searches the string for a specified value and returns the
last position of where it was found
rjust() Returns a right justified version of the string
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a
list
rstrip() Returns a right trim version of the string
split() Splits the string at the specified separator, and returns a
list
splitlines() Splits the string at line breaks and returns a list
startswith() Returns true if the string starts with the specified value
strip() Returns a trimmed version of the string
swapcase() Swaps cases, lower case becomes upper case and vice
versa
title() Converts the first character of each word to upper case
translate() Returns a translated string
upper() Converts a string into upper case
zfill() Fills the string with a specified number of 0 values at the
beginning
List Operators
Python Results Description
Expression
len([1, 2,3]) 3 Length
[1, 2, 3] + [4, 5,6] [1, 2, 3, 4, 5, 6] Concatenation
['Hi!'] * 4 ['Hi!', 'Hi!','Hi!', Repetition
'Hi!']
3 in [1, 2,3] True Membership
for x in [1, 2,3]: 1 23 Iteration
printx,
L=[24,35,46,78]
Python Results Description
Expression
L[2] [46] Offsets start at
zero
L[-3] [35] Negative: count
from the right
L[1:] [35,46,78] Slicing fetches
sections
List General Functions
Sr.No. Function with Description
1 cmp(list1, list2)Compares elements of both lists.
2 len(list)Gives the total length of the list.
3 max(list)Returns item from the list with max value.
4 min(list)Returns item from the list with min value.
5 list(seq) Converts atuple in to list.
List specific functions
Sr.No. Methods withDescription
1 list.append(obj)Appendsobjectobjtolist
2 list.count(obj)Returnscountofhowmanytimesobjoccursin list
3 list.extend(seq)Appendsthecontentsofseqto list
4 list.index(obj)Returnsthelowestindexinlistthatobjappears
5 list.insert(index,obj)Insertsobjectobjintolistatoffsetindex
6 list.pop(obj=list[-1])Removesandreturnslastobjectorobj fromlist
7 list.remove(obj)Removesobjectobjfromlist
8 list.reverse()Reversesobjectsoflistin place
9 list.sort([func])Sortsobjectsoflist,usecomparefuncif given
Python Dictionary Methods
Method Description
clear() Removes all items from the dictionary.
copy() Returns a shallow copy of the dictionary.
fromkeys(seq[, v]) Returns a new dictionary with keys from seq and value equal
to v (defaults to None).
get(key[,d]) Returns the value of the key. If the key does not exist, returns
d (defaults to None).
items() Return a new object of the dictionary's items in (key, value)
format.
keys() Returns a new object of the dictionary's keys.
pop(key[,d]) Removes the item with the key and returns its value or d if
key is not found. If d is not provided and the key is not found,
it raises KeyError.
popitem() Removes and returns an arbitrary item (key, value). Raises
KeyError if the dictionary is empty.
setdefault(key[,d]) Returns the corresponding value if the key is in the dictionary.
If not, inserts the key with a value of d and returns d (defaults
to None).
update([other]) Updates the dictionary with the key/value pairs from other,
overwriting existing keys.
values() Returns a new object of the dictionary's values