teach-ict.com logo

THE education site for computer science and ICT

2. String assignment

The first step in using strings is to get the program to recognise which variables are meant to be strings.

In Python, assignment is done using the "=" operator, not the "?" operator you might have been using in pseudocode.

Strings are assigned by putting the characters you want in the string, inbetween a pair of speech marks, like this:

MyLargeString = "Hello this is a string"

Strings can also have numbers inside of them. Once inside a string, the numbers are handled in the same way as any other set of characters.

You cannot carry out any calculations on numbers stored in a string without first converting them into a different data type, such as integer or floating point number. This is called 'casting'

A telephone number would always be stored as string data as it is very unlikely that any mathematical operations would be performed on it:

MyTelephoneNumber = "01234 567890"

Strings can be input by the user and stored in a string variable.

Python uses the "input" keyword, which works very similarly to the USERINPUT keyword used in pseudocode.

For example:-

                  YourName = input('Please enter your first name')

This displays a prompt on the screen asking the user to enter a string. When they do, it is stored as the YourName variable.

 

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: What is a string variable?