Preview

11 - String Manipulation #2

 1. Characters in a string can be accessed using the standard [ ] syntax, and like Java and C++, Python uses zero-based indexing, so if the str is 'hello' str[2] is:

  h

  o

  e

  l

 2. What is the output of the following code?
s = 'hi'
       ## 2
print s + ' there'  ## hi there

  hi

  there

  hi there

  None of the above

 3. Here we have used a negative index. What does this output?
a = "Python string"
print(a[-1])

  o

  y

  Error - negative indexes cannot be used

  g

 4. What does the following program do?
def add_string(str1):
  length = len(str1)

  if length > 2:
    if str1[-3:] == 'ing':
      str1 += 'ly'
    else:
      str1 += 'ing'

  return str1
print(add_string('ab'))
print(add_string('abc'))
print(add_string('string'))

  All of the above

  If the given string already ends with 'ing' then add 'ly' instead.

   If the string length of the given string is less than 3, leave it unchanged.

  adds 'ing' at the end of a given string (length should be at least 3).

 5. A unicode string is a different type of object from regular "str" string.

  FALSE

  TRUE

 6. What is the output of the following code (for loop on a string)
string="hello world"
for a in string:
    print a

  It is not possible to use a for loop on a string

  It will print the word: "hello"

  It will print out the whole string in one go: "hello world"

  It will print out every single letter in the string "hello world" to the screen (seperately)

 7. In the following program, if the substring is not present, the program will return:
message6 = "Hello World"
message6b = message6.find("squirrel")
print(message6b)

  -1

  1

  2

  0

 8. What is the output of the following program on which a start and end location have been used to manipulate the string?
message="Thisisaverylongstring"
startLoc = 2
endLoc = 8
message = message[startLoc: endLoc]
print(message)

  verylongstring

  avisis

  isisav

  Thisisavery

 9. Analyse the following code that uses the 'len' method. What does it do?
def find_longest_word(words_list):
    word_len = []
    for n in words_list:
        word_len.append((len(n), n))
    word_len.sort()
    return word_len[-1][1]

print(find_longest_word(["Python", "Testandtrackio", "Ireallylovelearning"]))

  It returns all the words in the list that are above a certain length

  It returns the word in the list that is the longest

  It returns all the words in the list as the len is set to -1

  It returns the last word in the list, which is what the -1 does.

 10. This program …
def sum_digits_string(str1):
    sum_digit = 0
    for x in str1:
        if x.isdigit() == True:
            z = int(x)
            sum_digit = sum_digit + z

    return sum_digit
     
print(sum_digits_string("123abcd45"))
print(sum_digits_string("abcd1234"))

  None of the above

  concatenates all the letters in the string and adds them together

  causes an error as it is not possible to add up the digits in a string

  computes the sum of digits of a given string