Homework5

.pdf

School

San Jose State University *

*We aren’t endorsed by this school

Course

30

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

4

Uploaded by MinisterBravery13388

Homework 05 Strings Harpreet Rathore 03/08/2022 Problem 5.1 - Escape Characters What are escape characters? Why do we need them? An escape character is a backslash \ followed by the character you want to insert and it is used to insert characters that are illegal in a string. Problem 5.2 - More Escape Characters What do the \n and \t escape characters represent? The \n character is called a new line character and it breaks a string into a new line just like hitting enter when typing regularly. The \t character is called a tab character and it puts indentation in between a string. Problem 5.3 - The Backslash Character How can you put a \ backslash character in a string? I am the "best" when it comes to Python. Problem 5.4 - Single and Double Quotes The string value "Howl's Moving Castle" is a valid string. Why isn’t it a problem that the single quote character in the word Howl's isn’t escaped? Howl's Moving Castle It is valid to have a single quotation character in a double quoted string and vice versa without escape command, but the type of quotations cannot be the same. Problem 5.5 - Print Statement In [1]: String = "I am the \"best\" when it comes to Python." print ( String ) In [6]: Value = "Howl's Moving Castle" print ( Value )
Printout the following statement: Hello, my name is your name here and I am your age here old. Define two variables, my_name and my_age and use two different ways of printing out your statement: 1. Using string concatenation. 2. Use the f-string method. See how much easier the f-string method is! Hello, my name is Harpreet Rathore and I am 21 years old Hello, my name is Harpreet Rathore and I am 21 years old I find the f-string method to be a lot easier as I can shorten my code and it is easily readable. Problem 5.6 - Useful String Methods Consider the string: str1 = 'the Hero gets IN trouble.' Now use string methods to accomplish the following: 1. Capitalize just the first letter of the sentence 2. Convert the string entirely to upper case 3. Convert the string entirely to lower case 4. Return a Boolean which returns True if the substring str1[4:7] is all in lower case 5. Return the index value where the substring 'ets' begins with respect to the original string. The hero gets in trouble. THE HERO GETS IN TROUBLE. the hero gets in trouble. False 10 Problem 5.7 - Print Formatting Use the f-string method to print the values of the physical constants pi and e. Use the math library and use math.pi and math.e for the two constants respectively. The neatly formatted table that should look the In [14]: my_name = "Harpreet Rathore" my_age = "21" sentence = "Hello, my name is " + my_name + " and I am " + my_age + " years old" print ( sentence ) txt = "Hello, my name is {} and I am {} years old" print ( txt . format ( my_name , my_age )) In [16]: str1 = 'the Hero gets IN trouble.' print ( str1 . capitalize ()) #1 print ( str1 . upper ()) #2 print ( str1 . lower ()) #3 print ( str1 [ 4 : 7 ] . islower ()) #4 print ( str1 . find ( 'ets' )) #5
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help