Worksheet - String Methods & Formatting
.docx
keyboard_arrow_up
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
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
Related Questions
Transcribed Image Text
Write a program that asks for the principal, the annual interest rate, and the number of times the interest is compounded. It should display a report similar to the following: Interest Rate: 4.25% Times Compounded: 12 Principal: $ 1000.00 Interest: 43.33 Final balance: $ 1043.33
arrow_forward
Option #1: String Values in Reverse Order
Assignment Instructions
Write a Python function that will accept as input three string values from a user. The method will return to the user a concatenation of the string values in reverse order. The function is to be called from the main method.
In the main method, prompt the user for the three strings.
arrow_forward
c code
Screenshot and output is must
arrow_forward
Domino's Time
Function Name: dominosTime()
Parameters: N/A
Returns: None
Description: During the summer, you ordered a lot of food from Domino's. Pizzas are $12, an
order of pasta is $6, and chicken wings are $8. Write a function that asks the user how many
of each food item they would like, and then print a response telling them what their order to-
tal will be. The order total should be an integer.
>>> dominosTime()
How many pizzas do you want? 3
How many orders of pasta do you want? 2
How many orders of chicken wings do you want? 2
By ordering 3 pizzas, 2 orders of pasta, and 2 orders of chicken wings, your
order total comes to $64.
arrow_forward
2 age > 10) || (age 55)
Question 8
Given strings str1 = "Hello" and str2 = "Goodbye ", what is the ending value of string str3?
str3 = strl + str2;
Error: Cannot add variables of type string
Goodbye Hello
Hello Goodbye
O HelloGoodbye
Question 9
arrow_forward
C# language
no lopping
use decision making and condtional operator
arrow_forward
C code
Screenshot and program is must
arrow_forward
#
# FUNCTION strlen(String value)
# Argument $a0 = value
# returns result in $v0
strlen:
li $v0, 0
next:
lb $t1, 0($a0)
beqz St1,
addi Sa0, Sa0, 1
j next
strlen_ret:
jr
SVO, SVO, 1
arrow_forward
calendar
allowed language: C Languange
The output and input should be the same as the samples.
arrow_forward
C# Language
You are tasked with writing a program to process students’ end of semester marks. For each student you must enter into your program the student’s id number (an integer), followed by their mark (an integer out of 100) in 4 courses. A student number of 0 indicates the end of the data. A student moves on to the next semester if the average of their 4 courses is at least 60 (greater than or equal to 60).
Write a program to read the data and print, for each student:
The student number, the marks obtained in each course, final mark of the student (average of all the marks), their final grade (A,B,C,D or F) and whether or not they move on to the next semester.
In addition, after all data entry is completed, print
The number of students who move on to the next semester and the number who do not.
The student with the highest final mark (ignore the possibility of a tie).
Grades are calculated as follows:
A = 70 and over, B = 60 (inclusive) -70, C = 50(inclusive)-60
D =…
arrow_forward
computer science
arrow_forward
C#
arrow_forward
C++ Programming Instructions
Write a program that prompts the user for a sequence of characters (all typed on a single line) and counts the number of vowels ('a', 'e', 'i', 'o', 'u'), consonants, and any other characters that appear in the input. The user terminates input by typing either the period (.) or exclamation mark character (!) followed by the Enter key on your keyboard.
Your program will not count white space characters.I.e., you will ignore white space characters.Though, the cin statement will skip white space characters in the input for you.
Your program will not be case-sensitive. Thus, the characters 'a' and 'A' are both vowels and the characters 'b' and 'B' are both consonants.
For example, the input How? 1, 2, 3. contains one vowel, two consonants, and six other kinds of characters.
IMPORTANT: Your program must use the while statement only when looping. I.e., do not use a for statement or any other looping statement.
Test 1
> run
Enter text:
.
Your sentence has 0…
arrow_forward
C++ Language
Write a single cout statement that prints exactly the following on the screen while there are no leading or trailing spaces "I am doing PF Exam and '\t' means four spaces".
arrow_forward
C++ language
arrow_forward
c## please
Write a Console Application that reads two integers, determines whether the first is a multiple of the second and displays the result. [Hint: Use the remainder operator.]
arrow_forward
c programing languge
Write a code that will do the following in order:
1- Take two strings from the user: s1 and s2
2- Compare the two strings and concatenate them such that the alphabetically first one goes first:
Ex:
Run 1:
s1: Amy
s2: Bruder
Result: Amy Bruder
Run 2:
s1: Baby
s2: Ahoy
Result: Ahoy Baby
arrow_forward
Circle Properties
Write a program that prompts for and accepts the diameter of a circle as a floating point number. The program should calculate and output the area and circumference of the circle.
A sample run of the program should look like this:
Enter circle diameter: 2.5
The area is 4.91 and the circumference is 7.85.
Use 3.14159 as your constant for pi.
global main ; exposes program entry point to the linkerextern printf ; declare external functionextern scanf
section .text ; start of code segment
main: push rbp ; preserve base pointer mov rbp,rsp ; copy stack pointer to base pointer
pop rbp ; restore base pointer mov rax, 0 ; exit status (0 = success) ret
section .data ; start of initialized data segment
section .bss ; start of uninitialized data segment
section .dataprompt db "Enter circle diameter: ", 0format db "The area is %0.2f and the circumference is %0.2f.", 0pi dq 3.14159…
arrow_forward
None
[] # Don't write code in this line
Q5: Complete the function that calculates the position of an object at any time t> 0. The initial position and the initial velocity (at time t=0) are denoted as so and v0 respectively. The object undergoes constant acceleration a, for any time t>0.
[ ]: def calculate_position (s0,u,a,t):
"given the initial conditions and acceleration, return the object's position.
Use the variable named 'position' to hold and return this value """
# YOUR CODE HERE
return position
I
↑↓古早
Check your code here "
Assign random input variables and check the answer"
arrow_forward
T F You cannot initialize a named constant that is declared with the const modifier.
arrow_forward
python
arrow_forward
Questions: 18 questions in total. 10 multiple choice questions, 5 missing word questions, and 3 programming questions.
Question 18
Rewrite the following statement with three statements.
double result += (double) a++ *b = 15;
a=a+1;
a+b=15;
result = result + 15;
result result + a + b + 15;
|result = result + (double) a * b;
b=15;
arrow_forward
All the Same Letter
Write a method to determine if all the letters in a String are the same letter.
public boolean sameLetter(String str)
{
//declare first character variable
//create statements to compare each character and include return statement
//create other return statement
}
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Related Questions
- Transcribed Image Text Write a program that asks for the principal, the annual interest rate, and the number of times the interest is compounded. It should display a report similar to the following: Interest Rate: 4.25% Times Compounded: 12 Principal: $ 1000.00 Interest: 43.33 Final balance: $ 1043.33arrow_forwardOption #1: String Values in Reverse Order Assignment Instructions Write a Python function that will accept as input three string values from a user. The method will return to the user a concatenation of the string values in reverse order. The function is to be called from the main method. In the main method, prompt the user for the three strings.arrow_forwardc code Screenshot and output is mustarrow_forward
- Domino's Time Function Name: dominosTime() Parameters: N/A Returns: None Description: During the summer, you ordered a lot of food from Domino's. Pizzas are $12, an order of pasta is $6, and chicken wings are $8. Write a function that asks the user how many of each food item they would like, and then print a response telling them what their order to- tal will be. The order total should be an integer. >>> dominosTime() How many pizzas do you want? 3 How many orders of pasta do you want? 2 How many orders of chicken wings do you want? 2 By ordering 3 pizzas, 2 orders of pasta, and 2 orders of chicken wings, your order total comes to $64.arrow_forward2 age > 10) || (age 55) Question 8 Given strings str1 = "Hello" and str2 = "Goodbye ", what is the ending value of string str3? str3 = strl + str2; Error: Cannot add variables of type string Goodbye Hello Hello Goodbye O HelloGoodbye Question 9arrow_forwardC# language no lopping use decision making and condtional operatorarrow_forward
- C code Screenshot and program is mustarrow_forward# # FUNCTION strlen(String value) # Argument $a0 = value # returns result in $v0 strlen: li $v0, 0 next: lb $t1, 0($a0) beqz St1, addi Sa0, Sa0, 1 j next strlen_ret: jr SVO, SVO, 1arrow_forwardcalendar allowed language: C Languange The output and input should be the same as the samples.arrow_forward
- C# Language You are tasked with writing a program to process students’ end of semester marks. For each student you must enter into your program the student’s id number (an integer), followed by their mark (an integer out of 100) in 4 courses. A student number of 0 indicates the end of the data. A student moves on to the next semester if the average of their 4 courses is at least 60 (greater than or equal to 60). Write a program to read the data and print, for each student: The student number, the marks obtained in each course, final mark of the student (average of all the marks), their final grade (A,B,C,D or F) and whether or not they move on to the next semester. In addition, after all data entry is completed, print The number of students who move on to the next semester and the number who do not. The student with the highest final mark (ignore the possibility of a tie). Grades are calculated as follows: A = 70 and over, B = 60 (inclusive) -70, C = 50(inclusive)-60 D =…arrow_forwardcomputer sciencearrow_forwardC#arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr