Worksheet - String Methods & Formatting

.docx

School

George Washington University *

*We aren’t endorsed by this school

Course

3119

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

1

Uploaded by BrigadierNarwhalPerson803

Report
Riti Mehra Worksheet – String Methods & Formatting 1 - Assume we have the following variable: name = ‘Joe’ Write the statement to print: Hello, Joe! (no space before the exclamation point) name = 'Joe' print(f'Hello, {name}!') 2 - Assume we have the following variables: name = “Joe”; gpa = 3.19837; id = 1234 Write the statement to print the following: Joe with id 1234 has a 3.19837 GPA. (use the format method or any other formatting option) name = "Joe" gpa = 3.19837 student_id = 1234 output_string = f"{name} with id {student_id} has a {gpa:.5f} GPA." print(output_string) 3 – Assuming we are using the str format method, what are the text options to perform the following actions: float with no rounding "{:.{}f}".format(float_value, 0) Float and round up to 3 decimal places "{:.3f}".format(float_value) Float and round up to 2 decimal places, and insert commas for > 999 "{:,.2f}".format(float_value) int with no special formatting "{}".format(int_value) int and insert commas for > 999 "{:,}".format(int_value) str with no special formatting "{}".format(string_value) 4 - What is the output of each: name = “joe” print(name.upper()) JOE x = “NONE” print(x.lower()) none name = ‘JANE’ print(name.title()) Jane name = ‘june’ print(name.title()) June 5 – What is a module? What is a function? What is the method? Module: A module is a file containing Python definitions and statements. It can define functions, classes, and variables that can be reused in other Python scripts. Function: A function is a block of reusable code that performs a specific task. It is a self-contained unit of code that can be called with a set of parameters to perform a specific action. Method: A method is a function that is associated with an object and is called on that object. It is similar to a function, but it is called on a specific instance of a class and may modify the object's state .
Discover more documents: Sign up today!
Unlock a world of knowledge! Explore tailored content for a richer learning experience. Here's what you'll get:
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help