These are the basic data types that constitute all of the more complex data structures in python. The basic data types are the following:
String variables are used to store textual data, characters and sequences of characters. Can be specified by surrounding some text with single '
or double "
quotes.
str_1 = "Hello World!"
print(str_1)
str_1 = "Hello World!"
print(str_1)
If we have a piece of text with multiple lines, then triple quotes (either '''
or """
) can be used to surround the text.
str_2 = """
(CNN)AirAsia Flight QZ8501 climbed rapidly before it crashed,
a top Indonesian official said Tuesday, according to The Jakarta Post.
Then the plane stalled, Transportation Minister Ignasius Jonan said
at a parliamentary hearing, according to the AFP and Reuters news agencies.
"The plane, during the last minutes, went up faster than normal
speed ... after then, it stalled. That is according to the data
from the radar," Jonan said, according to the news agencies.
"""
print(str_2)
str_3 = """
If we want to have multiple lines in the string
then we can use triple quotes: This is a multiline
string!
"""
print(str_3)
We have seen some common operations on numeric variables. Now, let’s discuss a few common operations on string variables. The complete list of string operations is available here.
The +
operator concatenates two strings:
# note that + concatenates strings
str1 = "hello"
str2 = "world"
message = str1 + " " + str2 + "!"
print(message)
Notice the difference of the +
operation when operating on text vs. numeric variables:
x = 41
x = x + 1
print(x)
x = "41"
x = x + "1"
print(x)
You are given a variable name = 'Panos'
and variable phone = '212-998-0803'
. Use the values of these variables to create and print the message Call Panos at 212-998-0803
.
name = "Panos"
phone = "212-998-0803"
# your code here (2 lines)
message = "Call " + name + " at " + phone
print(message)
len(str)
: length of a string, number of characters
word = "Python is the word. And on and on and on...."
print(len(word))
print(word)
print("The length of the text above is ", len(word), "characters")
You are given a brief news article, stored in the variable news
. Compute the length of the article, in characters.
news = """
Technology stocks are suffering one of their worst beatings in years,
as investors reassess a sector that has been considered the growth
engine of the global economy but now faces the prospect of greater
regulatory scrutiny. The tech-heavy Nasdaq Composite Index
fell 2.9% Tuesday. That selloff carried over to the broader market,
where the S&P 500 index slumped 1.7%. The Dow Jones Industrial
Average fell 1.4%, giving back some of Monday’s 2.8% rebound.
"""
print(len(news))
print(news)
print("The length of the text above is", len(news), "characters")
Now let's see how we can change the capitalization of the text. This will also be our first, informal, introduction to a new type of function.
str.upper()
: returns an uppercase version of a stringstr.lower()
: returns a lowercase version of a stringprint(news.lower())
print(news.upper())
# Notice that the original 'news' variable did not change
print(news)
# If we want to keep the changed-case version,
# we need to store it in a variable
l_news = news.lower()
print(l_news)
The .lower()
is a function that applies only to string variables. Notice the different way that we apply the function, compared to, say, the len(news)
above. In this case, we have a string variable (news
), then we have a dot (.
), and then we put the name of the function: (lower()
). Notice that we have the two parentheses at the end, which indicate that the function does not have any arguments, except for the news
variable. Notice that if we omit the parentheses, we will get a strange result:
news.lower