hi = 'hello world'
len(hi)
11
len("'hello world'")
13
len('2112')
4
'hello'[0]
'h'
'hello'[1]
'e'
'hello'[2]
'l'
'hello'[3]
'l'
'hello'[4]
'o'
'hello'[5]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) /tmp/ipykernel_5855/2204331286.py in <module> ----> 1 'hello'[5] IndexError: string index out of range
'hello world'[0:3]
'hel'
'2112'[0:3]
'211'
''[0:3]
''
'hello'[0:1]
'h'
''[0]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) /tmp/ipykernel_5855/1334571462.py in <module> ----> 1 ''[0] IndexError: string index out of range
''[0:1]
''
'hello world'[0:500]
'hello world'
'hello world'[450:500]
''
'hello world'[:3]
'hel'
'hello world'[1:]
'ello world'
'hello world'[:5]
'hello'
'hello world'[6:]
'world'
greeting = 'hello world'
greeting[0].upper() + greeting[1:] #x.upper() is a method that makes the current string an uppercase
'Hello world'
'abcdefgh'[0:9:2] #from 0 to 9 -withought 9- taking step by 2
'aceg'
'abcdefgh'[::2]
'aceg'
'0123456789'[::2]
'02468'
'hello world'[-1]
'd'
hi[-1:]
'd'
hi[-5]
'w'
hi[-5:]
'world'
wyatt = 'They flee from me that sometime did me seek / With naked foot, stalking in my chamber.'
'HELLO World'.lower() #makes all the character lowercase
'hello world'
original = 'Burma Shave'
lowercase = original.lower()
lowercase
'burma shave'
original
'Burma Shave'
wyatt = wyatt.lower()
wyatt
'they flee from me that sometime did me seek / with naked foot, stalking in my chamber.'
for c in wyatt:
print(c)
t h e y f l e e f r o m m e t h a t s o m e t i m e d i d m e s e e k / w i t h n a k e d f o o t , s t a l k i n g i n m y c h a m b e r .
for i in range(len(wyatt)): #==range(86)
print(wyatt[i])
t h e y f l e e f r o m m e t h a t s o m e t i m e d i d m e s e e k / w i t h n a k e d f o o t , s t a l k i n g i n m y c h a m b e r .
for i in range(len(wyatt)):
print(i,wyatt[i]) # i is being used as an index here.
0 t 1 h 2 e 3 y 4 5 f 6 l 7 e 8 e 9 10 f 11 r 12 o 13 m 14 15 m 16 e 17 18 t 19 h 20 a 21 t 22 23 s 24 o 25 m 26 e 27 t 28 i 29 m 30 e 31 32 d 33 i 34 d 35 36 m 37 e 38 39 s 40 e 41 e 42 k 43 44 / 45 46 w 47 i 48 t 49 h 50 51 n 52 a 53 k 54 e 55 d 56 57 f 58 o 59 o 60 t 61 , 62 63 s 64 t 65 a 66 l 67 k 68 i 69 n 70 g 71 72 i 73 n 74 75 m 76 y 77 78 c 79 h 80 a 81 m 82 b 83 e 84 r 85 .
list(range(len(wyatt)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85]
for i in range(len(wyatt)):
print(wyatt[i:i+2])
th he ey y f fl le ee e f fr ro om m m me e t th ha at t s so om me et ti im me e d di id d m me e s se ee ek k / / w wi it th h n na ak ke ed d f fo oo ot t, , s st ta al lk ki in ng g i in n m my y c ch ha am mb be er r. .
pairs = 0
for i in range(len(wyatt)):
if wyatt[i:i+2] == 'ee':
pairs = pairs + 1
pairs
2
pairs = 0
for i in range(len(wyatt) -1):
if wyatt[i] == wyatt[i+1]:
pairs = pairs +1
pairs
3
#exercise 8-1
def twin(string):
string = string.lower()
pairs=0
for i in range(len(string)-1):
if string[i] == string[i + 1]:
pairs = pairs + 1
print(string[i:i+2])
print(pairs)
phrase = 'Oolong'
twin('Oolong')
oo 1
p2 = 'hello boo, I loov yoo'
twin(p2)
ll oo oo oo 4
text = 'All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.'
text
'All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.'
len(text)
170
import nltk
text.split(' ') #the method split() divides a string into a list of strings
['All', 'human', 'beings', 'are', 'born', 'free', 'and', 'equal', 'in', 'dignity', 'and', 'rights.', 'They', 'are', 'endowed', 'with', 'reason', 'and', 'conscience', 'and', 'should', 'act', 'towards', 'one', 'another', 'in', 'a', 'spirit', 'of', 'brotherhood.']
text #the value of the text didn't change
'All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.'
len(text.split(' '))
30
text.split('and') #split without the words 'and'
['All human beings are born free ', ' equal in dignity ', ' rights. They are endowed with reason ', ' conscience ', ' should act towards one another in a spirit of brotherhood.']
hi = 'hello world'
hi.split(' ')
['hello', '', '', 'world']
'hello world'.split(' ')
['hello', '', '', 'world']
hi.split() #this one uses whitespace to divide
['hello', 'world']
text.split()
['All', 'human', 'beings', 'are', 'born', 'free', 'and', 'equal', 'in', 'dignity', 'and', 'rights.', 'They', 'are', 'endowed', 'with', 'reason', 'and', 'conscience', 'and', 'should', 'act', 'towards', 'one', 'another', 'in', 'a', 'spirit', 'of', 'brotherhood.']
names = ['Bob','Carol','Ted','Alice']
'&'.join(names)
'Bob&Carol&Ted&Alice'
' ' .join(names)
'Bob Carol Ted Alice'
','.join(names) # is a method of the strings that is used to do the joining
'Bob,Carol,Ted,Alice'
names.join('&')
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /tmp/ipykernel_8614/2601750644.py in <module> ----> 1 names.join('&') AttributeError: 'list' object has no attribute 'join'
names
['Bob', 'Carol', 'Ted', 'Alice']
' & '.join(names)
'Bob & Carol & Ted & Alice'
' and/or '.join(names)
'Bob and/or Carol and/or Ted and/or Alice'
'\n'.join(names)
'Bob\nCarol\nTed\nAlice'
print('\n'.join(names))
Bob Carol Ted Alice
print(' exists.\n'.join(names))
Bob exists. Carol exists. Ted exists. Alice
print(' exists.\n'.join(names) + ' exists.')
Bob exists. Carol exists. Ted exists. Alice exists.
for person in names:
print(person + ' exists.')
Bob exists. Carol exists. Ted exists. Alice exists.
sorted(names)
['Alice', 'Bob', 'Carol', 'Ted']
names
['Bob', 'Carol', 'Ted', 'Alice']
names.sort() # it sorts the list in place, in other words in the current list
names
['Alice', 'Bob', 'Carol', 'Ted']
# [Εxercise 8-2]
def same_last(s1,s2):
if s1[-1] == s2[-1]:
return 'True'
else:
return 'False'
same_last('la','lo')
'False'
same_last('radar','floor')
'True'
def same_last(s1,s2):
return s1[-1] == s2[-1]
same_last('maria','christina')
True
same_last('mario','giorgio')
True
same_last('pencil','draw')
False
#[Exercise 8-3]
def count_spaces(string):
i=0
number = 0
for i in range(len(string)):
if string[i] == ' ':
number = number + 1
i = i+1
return number
t ='και εγω που παντα ηθελα μεσα σου να ζησω'
count_spaces(t)
8
t2='σφαδάζω κάτω από έναν ξένο αστερισμό'
count_spaces(t2)
5
t3='ηρθα εδω για να υποφερω, να αγαπησω και να χαθω'
count_spaces(t3)
9
# [Exercise 8-4]
def count_nonspaces(string):
i=0
number = 0
for i in range(len(string)):
if string[i] != ' ':
number = number + 1
i = i+1
return number
count_nonspaces(t)
32
count_nonspaces(t2)
31
len(t) - count_space(t)
32
len(t2) - count_spaces(t2)
31
'hello'
'hello'
'hello'.upper()
'HELLO'
#exercise 8-5
def initials(string):
x = ''
for s in string:
if s == s.upper():
x = x + s
return x
initials('Neon Slovenian Kunst')
'N S K'
initials('Willem De Kooning Academie')
'W D K A'
initials('Sports Analogies')
'S A'
#Exercise 8-6
vowels = ['a','e','i','o','u']
def devowel(string): #this one doesn't work
string.split('a','e')
hi = 'hello world'
1 + 1
2
def devowel(string):
new = ''
for s in string:
if not s in ('a','e','i','o','u') :
new = new + s
return new
'hello' + 'o'
'helloo'
devowel(hi)
'hll wrld'
hi[0]
'h'