Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Read a String from a File Create a new text file in your prac _ 0 9 directory called name.txt . Type only your own

Read a String from a File
Create a new text file in your prac_09 directory called name.txt .
Type only your own name in the file.
Write a program in question_4() that reads this name.txt file and greets you, example:
Greetings Lindsay!
Note that since we want the entire contents of the file in one string, we don't need any kind of loop, we can just read
the file, like:
in_file = open("name.txt","r")
text = in_file. read()
Did it work?
If you got something like
Greetings Lindsay
!
Then what happened is the text variable includes the new line character ( In ) in your file.
You can solve this problem in two ways.
Edit the file so there's no new line
Strip the new line character (any whitespace from the ends of the line) from text with:
text = in_file.read ()*strip()
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

More Books

Students also viewed these Databases questions

Question

On what basis might Cameron maintain a suit against Anderson?

Answered: 1 week ago