Week 2 - Strings 

by Kevin SiswandiDec 22, 2020
1 like0 duplicates
Share
Twitter iconTwitter
Facebook iconFacebook
Linkedin
Email
Copy link
Save as PDF
    No headers found
# Methods # Methods are functions that belong to an object # Methods are specific to the data type for a particular variable sample_string = "I Love Programming" sample_string.islower()
temp = sample_string.lower() temp.islower()
sample_string.count('m') # count how many occurences of the letter 'm' in the string.
print(sample_string) print(sample_string[3]) sample_string.find('o') # find the index of the first occurence of 'o' in the string
# The format method for strings number_of_laptops = 27 print("Antonio has {} laptops.".format(number_of_laptops)) animal = "dragon" action = "fly" print("Does your {} {}?".format(animal, action))

Advanced concept: each of these methods accepts the string itself as the first argument of the method. However, they also could receive additional arguments, that are passed inside the parentheses.

No professional has all the methods memorized, you always refer to the documentation. Documentation on string methods https://docs.python.org/3/library/stdtypes.html#string-methods

Documentation on the format syntax #https://docs.python.org/3.6/library/string.html#format-string-syntax

# Exercise: fix a string some_quote = 'You\'ll do what you can do--and won\'t do what you can\'t do.' print(some_quote)
# Joey, Leslie, and Antonio have worked hard and generated some sales revenue for their company. # Calculate and print the total sales from the data provided. # Print out a string of the form "Total sales: xxx", where xxx will be the actual total # You’ll need to change the type of the input data in order to calculate that total. joey_sales = "121" leslie_sales = "105" antonio_sales = "110" # TODO: Print a string with this format: Total sales: xxx total_sales = int(joey_sales) + int(leslie_sales) + int(antonio_sales) print("Total Sales: " + str(total_sales))
# .title method "Juan leal antonio ruiz".title()
# .split method # Pangram contains all letters of the english alphabet a-z. sample_str = "The quick brown fox jumps over the lazy dog." # pangram sample_str.split()
another_str = "apple,banana,coconut,durian" another_str.split(sep=",")
# Write a log message for a server # using the data provided username = "Joey" timestamp = "16:50" url = "http://evergreeninstitute.com/staff/courses/english" # TODO: print a log message using the variables above. # The message should have the same format as this one: # "Leslie accessed the site http://evergreeninstitute.com/staff/course/python at 15:00." message = "{} accessed the site {} at {}".format(username,url,timestamp) print(message)
given_name = "Antonio" middle_names = "Juan Leal" family_name = "Ruiz" #TODO: find the length of Antonoio's name, also make sure to count spaces #name_length = len(given_name)+len(middle_names)+len(family_name)+2 name_length = len(given_name + middle_names + family_name ) + 2 print(name_length)
1/0
len("abc"
len(1)

Recommended on Deepnote

Stock Market Analysis

Stock Market Analysis

Last update 3 months ago
The 10 Best Ways to Create NumPy Arrays

The 10 Best Ways to Create NumPy Arrays

Last update 4 months ago
Wide Residual Networks

Wide Residual Networks

Last update 4 months ago