Methods Flashcards
(22 cards)
String.upper()
Makes the first letter upper case
Method to append an element to the end of the list
list.append(element)
Set method
set() method is used to create a new set object. A set is an unordered collection of unique elements.
Ways to use set method
- create a set from a list.
- create a set from a dictionary. Converts only keys.
A way to add element to a string
string += element
How to convert integer into a string
str(integer)
Method for lower case
string.lower()
replace method
Is a built in string method used to replace occurrences of a substring with a new string within a given string
Replace method syntax
string.replace(old_substring, new_substring, count)
count is optional. The max number of occurrences of old string to replace.
Ways to get rid of white space in a string
- with string.replace() method. Removes all white spaces
- string.strip() method - removes space at the beginning of a string and at the end
- str.lstrip(), str.rstrip() - removes it from left or right side
How to convert a list of strings into a string
With join method
str.join(lst)
How to convert a list with int to a string
With join() method
Example: lst =[…..]
str1= “ “.join([str(elem) for elem in lst])
How to turn a sentence into an array
sentence.split()
It can’t be combined with other methods
Length of an element
len(element)
Get the type of an element
type(element)
if type(element) == int:
How to reverse a string
element[::-1]
Add element to a set
set.add(element)
Check if an element in a set
element in mySet:
True/False
Length of a set
len(mySet)
Remove a specific element from a set
mySet.discard(element)
How to add an element to a dictionary of lists
dictionary[key_of_a_list].append(element)
Example of enumerate function use
Add