String Handling Operations In Programming

One of the most common operations in programming is to manipulate string inputs. This is known as String Handling as we are using our programs to analyse and adjust data.

There are a number of diffent ways that we can analyse a string including:

Its length

The position of a character

Extracting a substring

After analysing a string for what it already contains, there are also ways to adjust and adapt a string for the needs of the program.

Casting

The act of casting is to transform one data type into another. If you are taking user input into your program, you will likely find that the program assumes that it is a string. If you actually wanted them to input a number, you can do one of two things. In python these would look like:

myNum = int(input("Enter a number"))

or

myNum = input("Enter a number")

 

if myNum.isnumeric() ==True:
   myNum = int(myNum)

This second version will check to see if the casting is possible, preventing errors if the data entered is not a number.

Whilst an IF statement will check that the entry is valid (known as validation), it’s only useful if you want to prevent the program from crashing by skipping the code that would cause the error. Most validation requires the user to re-enter the data repeatedly until they get it right.

Find the syntax error in the code below, then try adapting the Python code below so that the user is asked to re-enter the number until it is an integer. You can run the code in the browser.

Concatenation

The term concatenation refers to creating a string by joining two or more strings together. In the pseudocode below, the string is joined to a string variable to create a single string.

name <-- INPUT
greeting <-- "Hello " + name
OUTPUT greeting

Concatenation is particularly useful when outputing messages to the user with calculated values.

Ord & Chr

Another way of handling strings in programming is to convert alphanumeric data into its numerical format in a character set. The Ord and Chr operators allow characters to be swapped between their text and number formats using the ASCII character set.

These two operators allow for more efficient validation of strings an creation of random letters. In the python code below, a word game uses Chr to pay against the user.

found = False

guessed = []

while found == False:

    guess = random.randint(97,122)

    while guess in guessed:

        guess = random.randint(97,122)

    print(“Is ” + Chr(guess) + ” your letter?)

Ord is just as powerful, and in the code below, it is used to create a range check for a letter instead of checking through the alphabet.

password = input("please enter your password: ")

if  (ord(password[0]) >=97) and (ord(password[0]) <=122):

    print("password starts with a lower case letter")

else:

    print("password invalid")

Try adapting the code below so that it checks that the password contains a lower case letter, an upper case letter, and a number. You can adjust and run the code in the browser.

Found this page helpful? Please consider sharing!

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on reddit
Share on whatsapp

Looking for More?

Sign in to access the following extras on this page for members:

Scribbl.it Notes

Programming

Flashcards

A Level Content

Looking for something else? Check back soon as new resources are added every week!

Not a member yet? Sign Up or Log In below