Chapter07. Advanced Strings, Lists
Chapter07. Advanced Strings, Lists
x = 10
print("x =", x)
x = 3.14
print("x =", x)
x = "Hello World!"
print("x =", x)
x = 10
x = 3.14
x = Hello World!
String
• Any kind of data can be stored in a variable.
String
String
• A string is a sequence of characters.
String
• Double quotes
• Single quotes
>>> "Hello"
'Hello'
>>> print(100+200)
300
>>> print("100"+"200")
100200
str()
Oh~
String concatenation
• To concatenate two strings -> + operator
>>> print("="*50)
==================================================
Include variable values in strings
• If you want to insert the value of a variable into a string
and print it out, use the ->% symbol.
s = "Monty Python"
print(s[6:10])
Pyth
Special strings
특수 문자열 의미
\t Tab character
\\ Backslash itself
Hello?
What is your name? Asema
Nice to meet you, Asema
Your name is: 5
How old are you? 21
You will be 22 next year.
>>>
Challenge problem
Ask the user other questions and respond in a friendly way. For example, you could ask about
hobbies like this:
"What are your hobbies?" "Watching movies."
"Yes, I like watching movies too."
Lab: Print the sum of year, month, and day
• Let's write a program that adds up the year, month, and
day of the day that the user inputs and displays them on
the screen, using a variable that stores a string.
import time
now = time.time()
thisYear = int(1970 + now//(365*24*3600))
print("This year is " + str(thisYear)+".")
Solution
import time
now = time.time()
thisYear = int(1970 + now//(365*24*3600))
print("This year is " + str(thisYear)+".")
Challenge problem
Is it possible to print a variable and a string at the same time using commas, such as
print("This year is", thisYear, ".") without using str()? Let's change the above program like this.
Which method is more convenient?
List
• In some cases, it is necessary to store multiple pieces of
data together.
Iron Man
Thor
heroes = ["Iron Man", "Thor", "Hulk", "Scarlet Witch"
Hulk
Scarlet Witch
Add from blank list
>>> heroes = [ ]
>>> heroes.append("Iron Man")
['Iron Man']
>>> heroes.append("Doctor Strange")
>>> print(heroes)
['Iron Man', 'Doctor Strange']
>>>
Meaning of dots
• In Python, everything is an object. An object is a group of
related variables and functions. In Python, lists are also
objects.
• When using something inside an object, you write the
name of the object followed by a period (.) and then the
name of the function.
heroes.append("Ion Man")
Accessing list items
>>> letters = ['A', 'B', 'C', 'D', 'E', 'F']
>>> print(letters[0])
A
>>> print(letters[1])
B
>>> print(letters[2])
C
Slicing
• Slicing is a technique for extracting multiple items from a
list at once.
>>> print(letters[:3])
['A', 'B', 'C']
>>> print(letters[3:])
['D', 'E', 'F']
>>> print(letters[:])
['A', 'B', 'C', 'D', 'E', 'F'] 리스트를 복사할 때 사용한다.
Change list items
>>> heroes.append("Spider-Man")
>>> print(heroes)
['Iron Man', 'Doctor Strange', 'Hulk', 'Scarlet Witch', 'Spider-Man']
if "superman" in heroes:
heroes.remove("superman")
del
• del deletes an item using its index.
Scarlett Witch
Browse the list
• Using index()
2
Visit the list
• pop() removes the last item from the list
Iron Man
Thor
Hulk
Scarlet Witch
Sort the list
##############################
# Proverb of the day #
##############################
There is nothing truly valuable that can be obtained without suffering.
quotes = []
quotes.append("Have a dream. Then you can overcome the difficult reality.")
quotes.append("Anger lives only in the hearts of fools..")
quotes.append("There is nothing truly valuable that can be obtained without suffering.")
quotes.append("When a person loves, everyone becomes a poet.")
quotes.append("The beginning is half.")
dailyQuote = random.choice(quotes)
print("############################")
print("# Today's Proverb #")
print("################################")
print("")
print(dailyQuote)
What we learned in this chapter