Wrap up Exercise - string

Class objectives

Syntax table

Keyword
description
replace(target_value, new_value)
Python string method replace() returns a copy of the string in which the occurrences of old have been replaced with new, optionally restricting the number of replacements to max.
split()
Python string method split() returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified), optionally limiting the number of splits to num.

Basic Exercises

Exercise 1: Print even numbers below 100

# Solution #1: Using while loop

# Solution #2: Using range(start, end, step)

Exercise 2: Print odd numbers below 50, in descending order

# Solution #1: Using while loop

# Solution #2: Using range(start, end, step)

Exercise 3: Print biggest number

Write a function that takes three integer parameters, return the biggest.

# Return the biggest number
def biggest_number(num1, num2, num3):

String Exercises

Exercise 1: Check if ‘is’ presents in the string

# Given a string
string = 'Python is a powerful programming language'

# Check if 'is' prsents in the string
def if_presents(substring):

Check if ‘me’ not presents in the string

Exercise 2: Get a new string from another string

# Given a function `replace(target, value)` and a string
string = 'Hello Python'

# Write a function to return 'Hello World'
def replace_string(string):

Exercise 3: Return substring

# Return the substring of the given string
string = 'Hello Python'
def return_substring(string):

# Return 'Hello P'
return_substring(string)

Exercise 4: Get certain values from a string

# Given a string
string = '10,20,30,40,50,60,70'

# Return '40,50'
def return_certain_values(string):

Homework

Write two functions one of which reverse the given string, another will check if the first string is reverse of the second string:

For Example:

# Reverse the given string
def reverse_string(string):

# CHeck if the first string is reverse of the second string
# Return True of False
def reverse_check(string1, string2):