0% found this document useful (0 votes)
7 views1 page

Coding Projects in Python (Dragged) 5

Uploaded by

Hayley Kelsey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Coding Projects in Python (Dragged) 5

Uploaded by

Hayley Kelsey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

26 FIRST STEPS

Working with strings


Coders use the word “string” for any data
Y 0
made up of a sequence of letters or other P T H N
characters. Words and sentences are
stored as strings. Almost all programs use
strings at some point. Every character that A string is simply a
sequence of characters.
you can type on your keyboard, and even
those you can’t, can be stored in a string. The quote marks show that
the variable contains a string.

1 Strings in variables
Strings can be put into variables. Type this
>>> name = 'Ally Alien'

code into the shell window. It assigns the >>> print(name)


string 'Ally Alien' to the variable name Ally Alien
and then displays it. Strings must always have
quotation marks at the beginning and end.
Hit the enter/return
Remember the
key to print the string.
quote marks.

2 Combining strings
Variables become really useful when you
>>> name = 'Ally Alien'

combine them to make new variables. If you >>> greeting = 'Welcome to Earth, '
add two strings together, you can store the >>> message = greeting + name
combination in a new variable. Try this out.
>>> print(message)
Welcome to Earth, Ally Alien
EXPERT TIPS
Length of a string The + symbol
joins one string
The quote marks to another.
You can use a handy trick, len(), to aren’t shown when
count the number of characters in a string you print a string.
(including the spaces). The command
len() is an example of what coders call a
function. (You’ll use lots of functions in this
book.) To find out how many characters He doesn’t have
there are in 'Welcome to Earth, Ally a clue!
Alien', type the line below into the Take me to your
leader...
shell once you’ve created the string, then
hit enter/return.

>>> len(message)
28

The number of
characters counted

You might also like