0% found this document useful (0 votes)
65 views7 pages

PYCS 03 - Strings and Data Types - Colaboratory

This document is a Python tutorial that discusses strings and data types. It explains that strings are used to represent text in Python and are defined using either single or double quotes. It also discusses different data types like integers, floats, and strings. It notes that operations like concatenation and addition work differently depending on the data types. The document provides examples of converting between data types using functions like str(), int(), and float().

Uploaded by

godspower bruno
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)
65 views7 pages

PYCS 03 - Strings and Data Types - Colaboratory

This document is a Python tutorial that discusses strings and data types. It explains that strings are used to represent text in Python and are defined using either single or double quotes. It also discusses different data types like integers, floats, and strings. It notes that operations like concatenation and addition work differently depending on the data types. The document provides examples of converting between data types using functions like str(), int(), and float().

Uploaded by

godspower bruno
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
You are on page 1/ 7

1/10/23, 1:15 PM PYCS 03 - Strings and Data Types - Colaboratory

A Python Introduction for New


(and not-so-new) Programmers
Part 03: Strings and Data Types

This Python Notebook is part of a sequence authored


by Timothy R James - feel free to modify, copy, share,
or use for your own purposes.

In this Notebook file, we explore the use of strings


and discuss how all of these different things are data
types.

Printing

Before we introduce strings - let's talk first about


printing. In the previous lesson, we just used the value
to print it out to the screen, like this:

some_number = 20
some_number

That will work fine for a single value, but when we


want to output more than a single value, it might
become confusing or present some challenges.
Fortunately, we can use print to output values.

print(some_number)

This has about the same effect as the code above,


but the good thing about print is that we can use it
over and over again, like this:

https://colab.research.google.com/drive/1xg3B13weRn7pZpSWkHr96_CDjMzJ2HYY?usp=sharing#scrollTo=MnmpX48rJZdC&printMode=true 1/7
1/10/23, 1:15 PM PYCS 03 - Strings and Data Types - Colaboratory

a = 5

b = 10.2

c = -8000

print(a)

print(b)

print(c)

Since it's more common to use print in Python


programs, we'll start using it for output in our code.

Strings

When we want to represent text in Python, we use


strings. A string is just a bunch of characters (letters,
numbers, or different symbols) that we associate
together.

Strings in Python usually start with a single quote and


end with a single quote. The single quotes aren't a
part of the string - they're just what we use to identify
where the string begins and ends.

a_string = 'This is a string.'

print(a_string)

Most often you see strings start and end with a single
quote, but they can also start and end with a double
quote.

another_string = "This is a string."

print(another_string)

When you run the code below, you can see that the
output of the two is the same - the string's content is
the same if you use single quotes or double quotes.

print(a_string)

print(another_string)

https://colab.research.google.com/drive/1xg3B13weRn7pZpSWkHr96_CDjMzJ2HYY?usp=sharing#scrollTo=MnmpX48rJZdC&printMode=true 2/7
1/10/23, 1:15 PM PYCS 03 - Strings and Data Types - Colaboratory

If you remember from the last lesson, we talked about


multi-line comments (remember, multi-line comments
started and ended with 3 single quotes). These
comments are actually valid strings in Python, so
you'll sometimes see this used when a really long
string is needed.

multi_line = '''Some really long string.

It can take up multiple lines.

The multi-line comment is really just a string and that's just valid syntax.'''

print(multi_line)

You can also concatenate strings using the plus sign


( + ). When you use the plus sign with numbers it
performs addition, but with strings it performs
concatenation - simply joining strings together.
Multiple strings can also be concatenated at once.

c = 'cat'

d = 'dog'

f = 'fish'

pet = d

sentence = 'My favorite pet is a ' + pet

print(sentence)

space = ' '

g = 'green'

sentence = 'The' + space + f + space + 'is' + space + g + '.'

A problem arises here, however; when we try to


concatenate a string and a number, we'll get an error.
You can try it below to see.

number = 100

s = 'One number is '

print(number + s)

https://colab.research.google.com/drive/1xg3B13weRn7pZpSWkHr96_CDjMzJ2HYY?usp=sharing#scrollTo=MnmpX48rJZdC&printMode=true 3/7
1/10/23, 1:15 PM PYCS 03 - Strings and Data Types - Colaboratory

If you ran that code, you'll see something like


unsupported operand type(s) for +: 'int' and
'str' - this basically means that a number and string
can't be concatenated together, at least not directly.
This introduces the concept of data types.

Data Types

Let's start by looking at the two numeric data types


we've already seen: decimal numbers, or floating
point numbers (called float in Python) and integer
numbers (called int in Python). For example, 7 is an
int , but 7.0 is a float .

A few example int values:

0
3
14171234
-1
-100

A few example float values:

0.0
1.0
1.75
-100000.0
1234234.142346709

The third data type we've seen so far is the string.


Data types will be important, because sometimes
Python will operate differently based on the data
types we're using. The + sign was the first example
of this - for strings, it concatenates, and for numbers, Dania Crumpton Grande
1:09 AM Today
it adds. (edited 1:17 AM Today)

Why is the answer 1250.0? Since that


would be consider a float, why wouldn't the
answer be 1250? Thank you
Type Conversions
https://colab.research.google.com/drive/1xg3B13weRn7pZpSWkHr96_CDjMzJ2HYY?usp=sharing#scrollTo=MnmpX48rJZdC&printMode=true 4/7
1/10/23, 1:15 PM PYCS 03 - Strings and Data Types - Colaboratory
Timothy James
1:17 AM Today

Fortunately, converting an int or a float to a string another_number is an int; however, when


is pretty easy. All you need to do is use str() - we divide it by 2, half_of_it is a float.

similar to how we used print() above. We need to


convert a number ( float or int ) to a string in
order to concatenate it with another string .

a_number = 3

a_string = 'one possible number is '

new_string = a_string + str(a_number)

print(new_string)

We can also use int() to convert a float to an int.


This simply removes the part to the right of the
decimal point. Try it below.

a_float = 3.7

an_int = int(a_float)

print(an_int)

Converting a string to a float or an int will allow


you to work with the value mathematically.
Tobechukwu Obi
5:54 AM Today
(edited 5:55 AM Today)
another_number_as_string = '2500'

There are 2 ways:

another_number = int(another_number_as_string)

half_of_it = another_number / 2
movie = title + str(sequel)

print(movie)

print(half_of_it)

title = 'Spider-man %s' % sequel

print(title)
One important point about str() - we don't typically
see this type of concatenation between strings and
Eyoas Bekele
integers. It's just too tedious. You don't want to 3:18 PM Yesterday
(edited 8:57 PM Yesterday)
always be using str() over and over again.
sum = str(value1) + str(value2) + str(value3)

print (sum)

Python lets us insert a value into a string by using %s # % this one doesn't work for me : # sum =
and the % operator. The two code blocks below do % value1 + % value2 + % value3 (error:
Line magic function `%` not found)
the same thing.
Timothy James
8:57 PM Yesterday
a_number = 50
a_string = 'Two times twenty five is ' + str(a_number)
i t( t i )
https://colab.research.google.com/drive/1xg3B13weRn7pZpSWkHr96_CDjMzJ2HYY?usp=sharing#scrollTo=MnmpX48rJZdC&printMode=true 5/7
1/10/23, 1:15 PM PYCS 03 - Strings and Data Types - Colaboratory
print(a_string) You don't mix "+" and "%" together; you can
use multiple "%" but we haven't covered
how to do that just yet.
a_number = 50
a_string = 'Two times twenty five is %s' % a_number Tobechukwu Obi
print(a_string) 9:10 AM Today

sum = str(value1) + str(value2) + str(value3)

print(sum)
In these lessons, we're likely to use a bit of str() and
a bit of % , but we'll use more % as we go on and and
need to work with strings in more complex ways.

Try It!

How would you show string manipulation in Python?


Let's use Python to work out a few examples below.

How can you make 'Spider-Man' into a sequel?


How can you find the sum of three numbers
reprented as strings?

title = 'Spider-Man'

sequel = 3

# how can you make this Spider-Man 3?

value1 = '9'
value2 = '27'
value3 = '33'
# what is the sum of these?

https://colab.research.google.com/drive/1xg3B13weRn7pZpSWkHr96_CDjMzJ2HYY?usp=sharing#scrollTo=MnmpX48rJZdC&printMode=true 6/7
1/10/23, 1:15 PM PYCS 03 - Strings and Data Types - Colaboratory

Colab paid products


-
Cancel contracts here

https://colab.research.google.com/drive/1xg3B13weRn7pZpSWkHr96_CDjMzJ2HYY?usp=sharing#scrollTo=MnmpX48rJZdC&printMode=true 7/7

You might also like