You are on page 1of 15

Strings

- arrays of bytes representiong unicode ch


- dose not have a ch data type
- a simple ch is a string with length of 1
- are immutable (can not be changed after created) - strings created after
concatenation and methodes are 'new' strings
Definition

- can be created using quotes, double quotes or triple quotes (for strings on multiple
lines)

myString = str()
myString = ' '
Empty * myString = " "
myString = """
"""

myString = "This is a string"


myString2 = " 100"
myString3 = " I am 28 years old "
Example myString4 = ' 3,14'
myString5 = ' ' - ⚠ is not emplty

myString[0] #T - first element is on position 0

⚠⚠⚠ Can't assign new value to a str position:


Accessing ch myString[0] = 'a' - error because str are immutable

" I am " "28 years old"


" I am " + "28 years old" - recommended

⚠⚠⚠ Can't concatenate diferent data types:


Concatenation "I am" + 28 - Error, can't do str+int
" I am " + str(28) -Correct

⚠ The spaces are also counted.


Length len(myString)
myString.__len__
s = 'string'
print(s[77]) -> IndexError (out of range)
print(s[4]) -> n
Index

lower()

upper()

capitalize()

count()

Methods
Methods

find()
rfind()

index()
rindex()

replace()

join()

strip()
lstrip()
rstrip()

split()
rsplit()

del

txt = "The best things in life are free!"


print("free" in txt)
Check * Output: True

print("expensive" not in txt)


Output: True

Reverse a *

* Slicing
Escape sequence
Formatting of *
Strings Lists
of bytes representiong unicode ch -collection of orderd data (items have a defined order
not have a ch data type and that order will not change, if not a method was
ple ch is a string with length of 1 applied)
mmutable (can not be changed after created) - strings created after - are mutable (can be changed using methods, without
enation and methodes are 'new' strings creating a new list)
- allow duplicate values.

e created using quotes, double quotes or triple quotes (for strings on multiple Empty list:
myList = []
myList = list()
ng = str()
ng = ' '
ng = " "
ng = """
"""

ng = "This is a string"
ng2 = " 100"
ng3 = " I am 28 years old "
ng4 = ' 3,14'
ng5 = ' ' - ⚠ is not emplty

ng[0] #T - first element is on position 0

n't assign new value to a str position:


ng[0] = 'a' - error because str are immutable

"28 years old"


+ "28 years old" - recommended

n't concatenate diferent data types:


+ 28 - Error, can't do str+int
+ str(28) -Correct

paces are also counted. len(l)


l.__len__
String)
ng.__len__ l = [] -> len() - 0
ing'
77]) -> IndexError (out of range)
4]) -> n

s = 'Hello WORLD'

print(s.lower()) - return the str in lowercase append()


Output: hello world

print(s.upper()) - return the str in uppercase clear()


Output: HELLO WORLD

print(s.capitalize()) - converts the first character to upper case copy()


Output: Hello world

Syntax: count(value, start, end) count()


- returns the number of times a specified value occurs in a string.
If requeste substr is missing will return 0.
- value - a string/ substring. The string to value to search for
- start - Optional. An Integer. The position to start the search.
Default is 0
- end - Optional. An Integer. The position to end the search.
Default is the end of the string

print(s.count('Hello'))
Output: 1

print(s.count('l'))
Output: 2

print(s.count('l', 3, 8))
Output: 1
Syntax: find(value, start, end) index()
- finds the first occurrence of the specified value. Return -1 if value
not found. Same as index(), but difference is that the index()
method raises an exception (error) if value not found.
- value - a string/ substring. Required. The value to search for
- start - Optional. Where to start the search. Default is 0
- end - Optional. Where to end the search. Default is to the end of
the string

print(s.find('Hello'))
Output: 0 - my substr starts with position 0

print(s.find('l'))
Output: 2 - count the first aparence of 'l'

replace() insert()

separator.join(sir) pop()

strip() remove()

reverse()

del s del

sort()

he best things in life are free!"


ree" in txt)
: True

expensive" not in txt)


: True
print(s[::-1])
Output: DLROW olleH
Lists Tuple
orderd data (items have a defined order -collection of orderd data (like lists but unchangeable)
will not change, if not a method was - are immutable (can not be changed after created)
- allow duplicate values
(can be changed using methods, without
w list)
ate values.

Empty tuple:
myTuple=()
myTuple = tuple()

Tuple with one element:


⚠ myTuple = (1 , ) -correct
⚠ myTuple = (1 ) - wrong, not a tuple, is a int

0
count()

index()
l= [1, 2, 3]
l.append(4)
print(l)
Output: [1, 2, 3, 4]
Set
-collection of unorderd data
- are mutable (can be changed after created, add, remove elements etc.)
- do not allow duplicats values
- unindexed (items do not have a defined order - every time has a different index)
- do not accept booleans if already exist 1 or 0 inside
- can not accept changable elem as lists or dict

Empty set:
mySet = set()

⚠ mySet = {} - wrong, this is a dictionary

Set with elements:


mySet = { 'a'}
mySet = {1 , 2, 3}

- can not access elements, no index available

⚠ mySet[1] -wrong -> TypeError

x.update(y)
{1, 2, 3, 'a', 'c', 'b'}

⚠ + is not accepted -> TypeError

print(x.__len__())
print(len(x))
- can not access elements, no index available

⚠ mySet[1] -wrong -> TypeError

x = {'a', 'b'}
add()
x.add(7) -> {'a', 'b', 7}

update()

clear()

Copy()

difference()

intersection()

union()
pop()

remove()

frozenset() - same as set but is immutable


- also can create a set of sets:
{frozenset({1, 2}), frozenset({3, 4})}
Dictionary
-collection of orderd (starting with python 3.7) data that
stores data in key-value pairs
- are mutable (changeable)
- do not allow duplicats keys (but values yes)

-presented by keys - values

You might also like