Homework5
.pdf
keyboard_arrow_up
School
San Jose State University *
*We aren’t endorsed by this school
Course
30
Subject
Computer Science
Date
Feb 20, 2024
Type
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
Related Questions
Word Separator Write a program that accepts as input a sentence in which all of the words are run together but the first character of each word is uppercase. Convert the sentence to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example the string “StopAndSmellTheRoses.” would be converted to “Stop and smell the roses.”
LINUX !#/bin/bash
arrow_forward
Allowed libraries:
arrow_forward
def swap_text(text):
Backstory:
Luffy wants to organize a surprise party for his friend Zoro and he wants to send a message to his friends, but he wants to encrypt the message so that Zoro cannot easily read it. The message is encrypted by exchanging pairs of characters.
Description: This function gets a text (string) and creates a new text by swapping each pair of characters, and returns a string with the modified text. For example, suppose the text has 6 characters, then it swaps the first with the second, the third with the fourth and the fifth with the sixth character.
Parameters: text is a string (its length could be 0)Return value: A string that is generated by swapping pairs of characters. Note that if the
Examples:
swap_text ("hello") swap_text ("Party for Zoro!") swap_text ("") def which_day(numbers):
→ 'ehllo'→ 'aPtr yof roZor!' → ''
length of the text is odd, the last character remains in the same position.
arrow_forward
08.
arrow_forward
public static String pancakeScramble(String text)
This nifty little problem is taken from the excellent Wolfram Challenges problem site where you can
also see examples of what the result should be for various arguments. Given a text string,
construct a new string by reversing its first two characters, then reversing the first three characters
of that, and so on, until the last round where you reverse your entire current string.
This problem is an exercise in Java string manipulation. For some mysterious reason, the Java
String type does not come with a reverse method. The canonical way to reverse a Java string
str is to first convert it to mutable StringBuilder, reverse its contents, and convert the result
back to an immutable string, that is,
str = new StringBuilder(str).reverse().tostring();
A bit convoluted, but does what is needed without fuss or muss. Maybe one day the Java strings will
come with the reverse method built in, just like the string data types of all sensible…
arrow_forward
public static String pancakeScramble(String text) This nifty little problem is taken from the excellent Wolfram Challenges problem site where you can also see examples of what the result should be for various arguments. Given a text string, construct a new string by reversing its first two characters, then reversing the first three characters of that, and so on, until the last round where you reverse your entire current string.This problem is an exercise in Java string manipulation. For some mysterious reason, the Java String type does not come with a reverse method. The canonical way to reverse a Java string str is to first convert it to mutable StringBuilder, reverse its contents, and convert the result back to an immutable string, that is,str = new StringBuilder(str).reverse().toString();
Here's the tester it must pass:
@Test public void testPancakeScramble() throws IOException {// Explicit test casesassertEquals("", P2J3.pancakeScramble(""));assertEquals("alu",…
arrow_forward
True or False: An Array of characters is a C-String?
arrow_forward
Linux !#bin/bash
NOT JAVA OR C++. I NEED LINUX SCRIPT
Word Separator Write a program that accepts as input a sentence in which all of the words are run together but the first character of each word is uppercase. Convert the sentence to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example the string “StopAndSmellTheRoses.” would be converted to “Stop and smell the roses.”
arrow_forward
language using - Java
arrow_forward
Need to write a program.
arrow_forward
What happens if you try to retrieve a character in a string with an incorrect index?
arrow_forward
Help
arrow_forward
write programs date form
arrow_forward
python:
def shakespeare_position(role, section): """ Question 2 - Regex
You are reading a Shakespeare play with your friends (as one frequently does) and are given a role. You want to know what line immediately precedes YOUR first line in a given section so that you are ready to go when it is your turn. Return this line as a string, excluding the character's name.
Lines will always begin with the character's name followed by a ':' and end in a "." or a "?" Each line is separated by a single space.
THIS MUST BE DONE IN ONE LINE.
""
Args: role (str) section (str)
Returns: str
section_1 = 'Benvolio: By my head, here come the Capulets. Mercutio: By my heel, I care not. ' + 'Tybalt: Gentlemen, good den - a word with one of you. Mercutio: And but one word with one of us?'
>>> shakespeare_position('Tybalt', section_1) 'By my heel, I care not.' >>> shakespeare_position('Mercutio', section_1) 'By my head, here…
arrow_forward
Computer Science Programming question C++ Code
arrow_forward
String.ParseInt(m); will convert the integer value of m to a String
True
False
arrow_forward
# Exercise 1: Ask for a birthday, a favorite gift, and their dream job is and display
# that information within a block in a fromated string
arrow_forward
Need help C Programming plz
arrow_forward
Clearer instructions
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Related Questions
- Word Separator Write a program that accepts as input a sentence in which all of the words are run together but the first character of each word is uppercase. Convert the sentence to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example the string “StopAndSmellTheRoses.” would be converted to “Stop and smell the roses.” LINUX !#/bin/basharrow_forwardAllowed libraries:arrow_forwarddef swap_text(text): Backstory: Luffy wants to organize a surprise party for his friend Zoro and he wants to send a message to his friends, but he wants to encrypt the message so that Zoro cannot easily read it. The message is encrypted by exchanging pairs of characters. Description: This function gets a text (string) and creates a new text by swapping each pair of characters, and returns a string with the modified text. For example, suppose the text has 6 characters, then it swaps the first with the second, the third with the fourth and the fifth with the sixth character. Parameters: text is a string (its length could be 0)Return value: A string that is generated by swapping pairs of characters. Note that if the Examples: swap_text ("hello") swap_text ("Party for Zoro!") swap_text ("") def which_day(numbers): → 'ehllo'→ 'aPtr yof roZor!' → '' length of the text is odd, the last character remains in the same position.arrow_forward
- 08.arrow_forwardpublic static String pancakeScramble(String text) This nifty little problem is taken from the excellent Wolfram Challenges problem site where you can also see examples of what the result should be for various arguments. Given a text string, construct a new string by reversing its first two characters, then reversing the first three characters of that, and so on, until the last round where you reverse your entire current string. This problem is an exercise in Java string manipulation. For some mysterious reason, the Java String type does not come with a reverse method. The canonical way to reverse a Java string str is to first convert it to mutable StringBuilder, reverse its contents, and convert the result back to an immutable string, that is, str = new StringBuilder(str).reverse().tostring(); A bit convoluted, but does what is needed without fuss or muss. Maybe one day the Java strings will come with the reverse method built in, just like the string data types of all sensible…arrow_forwardpublic static String pancakeScramble(String text) This nifty little problem is taken from the excellent Wolfram Challenges problem site where you can also see examples of what the result should be for various arguments. Given a text string, construct a new string by reversing its first two characters, then reversing the first three characters of that, and so on, until the last round where you reverse your entire current string.This problem is an exercise in Java string manipulation. For some mysterious reason, the Java String type does not come with a reverse method. The canonical way to reverse a Java string str is to first convert it to mutable StringBuilder, reverse its contents, and convert the result back to an immutable string, that is,str = new StringBuilder(str).reverse().toString(); Here's the tester it must pass: @Test public void testPancakeScramble() throws IOException {// Explicit test casesassertEquals("", P2J3.pancakeScramble(""));assertEquals("alu",…arrow_forward
- True or False: An Array of characters is a C-String?arrow_forwardLinux !#bin/bash NOT JAVA OR C++. I NEED LINUX SCRIPT Word Separator Write a program that accepts as input a sentence in which all of the words are run together but the first character of each word is uppercase. Convert the sentence to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example the string “StopAndSmellTheRoses.” would be converted to “Stop and smell the roses.”arrow_forwardlanguage using - Javaarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT