PYCS 03 - Strings and Data Types - Colaboratory
PYCS 03 - Strings and Data Types - Colaboratory
Printing
some_number = 20
some_number
print(some_number)
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)
Strings
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
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)
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 + '.'
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
Data Types
0
3
14171234
-1
-100
0.0
1.0
1.75
-100000.0
1234234.142346709
a_number = 3
a_string = 'one possible number is '
new_string = a_string + str(a_number)
print(new_string)
a_float = 3.7
an_int = int(a_float)
print(an_int)
another_number = int(another_number_as_string)
half_of_it = another_number / 2
movie = title + str(sequel)
print(movie)
print(half_of_it)
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
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!
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
https://colab.research.google.com/drive/1xg3B13weRn7pZpSWkHr96_CDjMzJ2HYY?usp=sharing#scrollTo=MnmpX48rJZdC&printMode=true 7/7