Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Exercise 6.1 Write a while loop that starts at the last character in the string and works its way backwards to the first character in the string, printing each letter on a separate line, except backwards.
- prog_lang='python3'
- i=len(prog_lang)
- while i>0:
- i-=1
- print(prog_lang[i],'\n')
- ------------------------------------------------------------------------------------------------
- #Exercise 6.2 Given that fruit is a string, what does fruit[:] mean?
- fruit='string'
- print(fruit[:])
- #full string
- ------------------------------------------------------------------------------------------------
- #Exercise 6.3 Encapsulate this code in a function named count, and generalize it so that it accepts the string and the letter as arguments.
- def count(word,l):
- count = 0
- for letter in word:
- if letter == l:
- count = count + 1
- print(count)
- userword='banana'
- userletter='a'
- count(userword,userletter)
- ------------------------------------------------------------------------------------------------
- #Exercise 6.4 There is a string method called count that is similar to the function in the previous exercise. Read the documentation of this method at docs.python.org/library/string.html and write an invocation that counts the number of times the letter a occurs in 'banana'.
- str_ing='banana'
- let_ter='a'
- print(str_ing.count(let_ter))
- ------------------------------------------------------------------------------------------------
- #Exercise 6.5
- str_ing='X-DSPAM-Confidence: 0.8475'
- str_ing=str_ing[str_ing.find(':')+1:]
- print('Converted string to float :',float(str_ing))
- print('Conversion check:',type(str_ing))
- ------------------------------------------------------------------------------------------------
- #Excersice 6.6 string methods
- word="orange"
- print('str.upper=',word.upper())
- print('str.lower=',word.lower())
- print('str.capitalize=',word.capitalize())
- a="chocolate"
- print('count of letter o in string a is:',a.count('o'))
- b='hello!'
- print('str.encode=',b.encode(encoding='utf-8'))
- print('str.find=',a.find('o',1,3))
- print("1+1 is {}".format(1+1))
- print('str.replace =',b.replace('!',' josh',3))
- print(a)
- print('str.join=',b.join('12345'))
- c=" how are you? \n"
- print('str.strip',c.strip())
- print('str.swapcase',c.swapcase())
- d="...,I am fine so.,"
- print('str.lstrip is',d.lstrip('hello.,'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement