chapter 6

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

my_str = 'http://reddit.com/r/python' print(my_str[17:])

/r/python

city = "San Diego San Diego" pos0 = city.rfind("ie") print(pos0) pos1 = city.rfind("ie", 2) print(pos1) pos2 = city.rfind("ie", 2, 6) print(pos2) pos3 = city.rfind("ie", 2, 7) print(pos3) pos4 = city.rfind("ie", 2, 20) print(pos4) pos5 = city.rfind("ie", 5, 20) print(pos5) pos6 = city.rfind("ie", 6, 20) print(pos6) pos7 = city.rfind("ie", 15, 20) print(pos7) pos8 = city.rfind("ie", 16, 20) print(pos8)

15 15 -1 5 15 15 15 15 -1

txt = "I love apples, apple are my favorite fruit!!!" num1 = txt.count("apple") print(num1) num2 = txt.count("favorite") print(num2) num3 = txt.count("!") print(num3) num4 = txt.count("!!") print(num4) num5 = txt.count("!!!") print(num5) num6 = txt.count("?") print(num6) print(txt)

3 1 1 0 I love apples, apple are my favorite fruit!!!

city = "San Diego San Diego" pos1 = city.find("ie", -17) print(pos1) pos2 = city.find("ie", -17, 6) print(pos2) pos3 = city.find("ie", 2, -12) print(pos3) pos4 = city.find("ie", 2, -1) print(pos4) pos5 = city.find("ie", 5, -1) print(pos5) pos6 = city.find("ie", 6, -1) print(pos6) pos7 = city.find("ie", 15, -1) print(pos7) pos8 = city.find("ie", 16, -1) print(pos8)

5 -1 5 5 5 15 15 -1

city = "San Diego San Diego" pos1 = city.find("ie", -17) print(pos1) pos2 = city.find("ie", -17, -13) print(pos2) pos3 = city.find("ie", -17, -12) print(pos3) pos4 = city.find("ie", -17, -1) print(pos4) pos5 = city.find("ie", -14, -1) print(pos5) pos6 = city.find("ie", -13, -1) print(pos6) pos7 = city.find("ie", -4, -1) print(pos7) pos8 = city.find("ie", -3, -1) print(pos8)

5 5 15 15 -1

'{x:.1f}'.format(x=5)

5.0

'{x:4.1f}'.format(x=5)

5.0

'{x:.3f}'.format(x=5)

5.000

'{x:.3f}'.format(x=5.25)

5.250

'{x:.3f}'.format(x=5.2589)

5.259

my_str = 'Agt2t3afc2kjMhagrds!' print(my_str[0:5:1])

Agt2t

city = 'Algiers' my_slice = city[0:6:3] print(my_slice)

Ai

#city = "Washington" #city = "Las Vegas" city = "San Diego" if city < "San Diego": print("Your city, " + city + ", comes before San Diego.") elif city > "San Diego": print("Your city, " + city + ", comes after San Diego.") else: print("All right, San Diego.")

All right, San Diego.

my_str = 'Agt2t3afc2kjMhagrds!' print(my_str[::2])

AttackMars

city = 'Budapest' city_slice = city[0:4] print(city_slice)

Buda

city = "San Diego" print(city[-5:-2]) print(city[-8:-1]) print(city[-1:-8]) print(city[:-2]) print(city[-8:])

Die an Dieg San Die an Diego

city = 'Dublin' city_slice = city[:5] print(city_slice)

Dubli

'1 2 3 4 5'.isdigit()

False

'jo' in 'Joseph'

False 'jo' (with a lowercase 'j') is not in 'Joseph' (with an uppercase 'J') case-sensitive

'Yankee Sierra' > 'Yankee Zulu'

False characters of both sides match until the second word. The first character of the second word on the left 'S' is not "greater than" (in ASCII value) the first character on the right side 'Z'

'HTTPS://google.com'.isalnum()

False isalnum() = True if all characters are lowercase or uppercase or numbers 0-9

'Hello' == 'Hello!'

False left hand string does not end with '!'

item_info = 'Headphones 17 13' item_tokens = item_info.split() item = item_tokens[0] quantity = item_tokens[1] price = item_tokens[2] print(item, 'stock:', quantity) print('Price:', price)

Headphones stock: 17 Price: 13

original_text = "SAN DIEGO" new_text = original_text.title() print(original_text) print(new_text) print(" san diego".title()) print(" SAN DIEGO".title())

SAN DIEGO San Diego San Diego San Diego

city = "San Diego" uppercase_city = city.upper() print(city) print(uppercase_city) print("San Diego".upper())

San Diego SAN DIEGO SAN DIEGO

city = "San Diego" lowercase_city = city.lower() print(city) print(lowercase_city) print("San Diego".lower())

San Diego san diego san diego

city = " San Diego " city1 = city.lstrip() city2 = city.rstrip() city3 = city.strip() print(city + "|^o^|") print(city1 + "|^o^|") print(city2 + "|^o^|") print(city3 + "|^o^|")

San Diego |^o^| San Diego |^o^| San Diego|^o^| San Diego|^o^|

'HTTPS://google.com'.startswith('HTTP')

True

'LINCOLN, ABRAHAM'.isupper()

True

'Yankee Sierra' > 'Amy Wise'

True first character of the left side 'Y' is "greater than" (in ASCII value) the first character of the right side 'A'

'\n \n'.isspace()

True newline and spaces = whitespace

'seph' in 'Joseph'

True substring 'seph' can be found starting at the 3rd position of 'Joseph'

song = "I scream; you scream; we all scream, for ice cream.\n" song.split('scream')

['I ', '; you ', '; we all ', ', for ice cream.\n']

song = "I scream; you scream; we all scream, for ice cream.\n" song.split('\n')

['I scream; you scream; we all scream, for ice cream.', '']

song = "I scream; you scream; we all scream, for ice cream.\n" song.split()

['I', 'scream;', 'you', 'scream;', 'we', 'all', 'scream,', 'for', 'ice', 'cream.']

oldstring = "a" newstring = oldstring * 8 print(oldstring) print(newstring) oldstring = "San Diego " newstring = oldstring * 8 print(oldstring) print(newstring)

a aaaaaaaa San Diego San Diego (+ 7 more times)

Print air_temperature with 1 decimal point followed by C. Sample output with input: 36.4158102 36.4C

air_temperature = float(input()) print('%.1fC'%air_temperature)

city = 'Dublin' my_slice = city[2:4] print(my_slice)

bl excluding index 4 (only 2 and 3)

color = 'cream' my_slice = color[0:-3] print(my_slice)

cr index -3 is e

city = "haha San Francisco" another_city = city.replace("Francisco", "Diego") print(city) print(another_city) third_city = city.replace("a", "o") print(city) print(third_city) third_city = city.replace("a", "o", 1) print(city) print(third_city) fourth_city = city.replace("a", "o", 3) print(city) print(fourth_city)

haha San Francisco haha San Diego haha San Francisco hoho Son Froncisco haha San Francisco hoha San Francisco haha San Francisco hoho Son Francisco

city = "San Diego" print(city[20]) print(city[:20])

index out of range San Diego

city = 'Dublin' my_slice = city[3:5] print(my_slice) city = 'Algiers' print(my_slice)

li li while city changed, my_slice remains the same

Write a statement that uses the join() method to set my_str to 'NewYork', using the list x = ['New', 'York']

my_str = ''.join(x)

Write a statement that uses the join() method to set my_str to 'images.google.com', using the list x = ['images', 'google', 'com']

my_str = '.'.join(x)

Assign number_segments with phone_number split by the hyphens. Sample output with input: '977-555-3221' Area code: 977

phone_number = input() number_segments = phone_number.split("-") area_code = number_segments[0] print('Area code:', area_code)

my_str = 'http://reddit.com/r/python' protocol = 'http://' print(my_str[len(protocol):])

reddit.com/r/python

city = "san diego" capitalize_city = city.capitalize() print(city) print(capitalize_city) print("san diego".capitalize()) print("SAN DIEGO".capitalize()) print(" san diego".capitalize())

san diego San diego San diego San diego san diego

Assign sub_lyric by slicing rhyme_lyric from start_index to end_index which are given as inputs. Sample output with inputs: 4 7 cow

start_index = int(input()) end_index = int(input()) rhyme_lyric = 'The cow jumped over the moon.' sub_lyric = rhyme_lyric[start_index:end_index] print(sub_lyric)

Write a statement that replaces the separators in the string variable title from hyphens (-) to colons (:)

title = 'Python-Lab-Warmup' tokens = title.split('-') title = ':'.join(tokens)

Complete the if-else statement to print 'LOL means laughing out loud' if user_tweet contains 'LOL'. Sample output with input: 'I was LOL during the whole movie!' LOL means laughing out loud.

user_tweet = input() if 'LOL' in user_tweet: print('LOL means laughing out loud.') else: print('No abbreviation.')

Assign decoded_tweet with user_tweet, replacing any occurrence of 'TTYL' with 'talk to you later'. Sample output with input: 'Gotta go. I will TTYL.' Gotta go. I will talk to you later.

user_tweet = input() decoded_tweet = user_tweet.replace("TTYL", "talk to you later") print(decoded_tweet)

address = 'www.google.com' separator = ';' address_tokens = address.split('.') print(separator.join(address_tokens))

www;google;com


Kaugnay na mga set ng pag-aaral

NSG 333 Ch 4- Common Reproductive Issues

View Set

AP Psychology: Unit 14 Test Review

View Set

Module 10 Computer Concepts Exam

View Set

Retail Management Chapter 5 Retail Market Strategy

View Set

Chapter 8: Overview of Network Security and Network Threats

View Set