Chapter 11

Task 1

The following is my execution of the len function

11.1: len.py

text = "Believe it or not this is a string"
print("Enter the text: " + text)

length = len(text)

print("Your text has " + str(length) + " characters.")

Output

>>> Enter the text: Believe it or not this is a string
>>> Your text has 34 characters.

Task 2

I also managed to convert a piece of text to be uppercase.

11.2: upper.py

# The text without any numbers or symbols
text = "hello"

output = text.upper()

print(output)

# The text with additional numbers and symbols
text = "123hello&&#$("

output = text.upper()

print(output)

Output

>>> HELLO
>>> 123HELLO&&#$(

As you can see above I also tested with alphanumeric characters and symbols. This was my conclusion.

The numbers and symbols stay the same but the alphabetic characters are made uppercase

What follows is a completed table of methods and outputs

MethodInputsOutputs
lower"Hello", "HELLO""hello", "hello"
capitalize"Hello", "HELLO""HELLO", "HELLO"
islower"hello", "HELLO", "Hello"true, false, false
isalpha"Hello", "Hello1", "111"true, false, false

Task 3

The following is the output of my slicing tests

FunctionResult
print(text[4])o
print(text[-3])r
print(text[2:7])llo W
print(text[3:])lo World
print(text[:4])Hell
print(text[1:-2])ello Wor
print(text[2:-5])llo
print(text[:-1])Hello worl

The program I used to get this table is as follows

Source: 11.3 slicing.py

text = "Hello World"

print(text[4])
print(text[-3])
print(text[2:7])
print(text[3:])
print(text[:4])
print(text[1:-2])
print(text[2:-5])
print(text[:-1])

Then I also tested left select, right select and half functions.

11.3: half.py

string = "Believe it or not this is a string"
print("Enter a string: " + string)

print(string[-int(len(string) / 2):])

Output

>>> Enter a string: Believe it or not this is a string
>>>  this is a string

11.3: left

from sys import argv


def parse_int_or(string: str, otherwise: int):
  try:
      return int(string)
  except ValueError:
      return otherwise


string = "Believe it or not this is a string"
print("Enter a string: " + string)
chars = parse_int_or(argv.pop(), 69)

print(string[:chars])

Output

>>> Enter a string: Believe it or not this is a string
>>> Believe it or not this is a string

11.3: right

from sys import argv


def parse_int_or(string: str, otherwise: int):
  try:
      return int(string)
  except ValueError:
      return otherwise


string = "Believe it or not this is a string"
print("Enter a string: " + string)
chars = parse_int_or(argv.pop(), 69)
end_chars = -chars

print(string[:chars])

Output

>>> Enter a string: Believe it or not this is a string
>>> Believe it or not this is a string

Task 4

InputOutput
apple0
pear2
lemon-1
banana1
GRAPE-1

The program I used to get the above table is as follows

11.4: searching.py

text = "Believe it or not this is a string"
print("Enter the text: " + text)

final = text.rfind("a")
count = text.count("a")
maximum = max(text)
minimum = min(text)
numeric = text.isnumeric()

print('Final:', final)
print('Count:', count)
print('Maximum:', maximum)
print('Minimum:', minimum)
print('Numeric:', numeric)

Output

>>> Enter the text: Believe it or not this is a string
>>> Final: 26
>>> Count: 1
>>> Maximum: v
>>> Minimum:  
>>> Numeric: False