String Manipulation & ASCII Art

asciiart

Learning Objectives


In this post we will focus on string manipulation techniques using five common techniques used in most procedural programming languages:

  • LEFT: to extract characters at the beginning of a string,

  • RIGHT: to extract characters at the end of a string,

  • LENGTH: to find out the number of characters in a string,

  • MID: to extract a set number of characters at a given position in a string,

  • LOCATE: to find if a substring is included in another string and if so return the position of this substring in the string.

LEFTRIGHTLENGTHMIDLOCATE
e.g. LEFT(myString, 5)

>>> to extract the first 5 characters of a string.

e.g. RIGHT(myString, 5)

>>> to extract the last 5 characters of a string.

e.g. LENGTH(myString)

>>> to retrieve the number of characters in a string.

e.g. MID(myString, 3, 5)

>>> to extract a set number of characters at a given position in a string.

e.g. LOCATE(“World”, “Hello World”)

>>> to find if a substring is included in another string and if so return the position of this substring in the string.
If the substring is not part of the string, the value -1 is returned.

Your Challenge


Look at the pixel art below. The user enters their name and the computer generates an ASCII art. However the name of the user does not always fit within this pixel art.

Your challenge is to change this code to make sure that the Ascii Art takes into consideration the number of characters in the user’s name to make sure the Ascii art always looks fine.

Share Button
Posted in Computer Science, Python - Intermediate, Python Challenges

Our Latest Book