Python Quick Question about Python concatenation

  • Thread starter Thread starter NATURE.M
  • Start date Start date
  • Tags Tags
    Python
AI Thread Summary
The discussion revolves around a programming problem involving string concatenation in Python. Given two strings, 'movies' and ' goto', the task is to concatenate them n times, with the stipulation that if n is negative, it behaves as if n is 0. The initial confusion stems from whether the output should be None or an empty string when n is 0 or negative. It is clarified that the correct output for these cases is an empty string, not None, as confirmed by testing in a Python interpreter. The conversation emphasizes understanding the behavior of string operations in Python, particularly regarding negative values for n.
NATURE.M
Messages
298
Reaction score
0

Homework Statement



Lets say we have two strings 'movies' and ' goto'. Now the question indicates to return this combination concatenated n times, except if n is negative, it is the same as if it were 0.

The Attempt at a Solution



Concatenation n times would yield: n*"movies goto"

Now if n is negative it performs in the same way you would expect 0 to perform, so would that just return None ?
Like if n = 0, or n = -4 should it just return None?
 
Technology news on Phys.org
NATURE.M said:

Homework Statement



Lets say we have two strings 'movies' and ' goto'. Now the question indicates to return this combination concatenated n times, except if n is negative, it is the same as if it were 0.

The Attempt at a Solution



Concatenation n times would yield: n*"movies goto"

Now if n is negative it performs in the same way you would expect 0 to perform, so would that just return None ?
Like if n = 0, or n = -4 should it just return None?

Don't you have a python interpreter to try it out in? "None" would be a plausible answer, but I don't think it does that. It returns an empty string. Also plausible.
 
Dick said:
Don't you have a python interpreter to try it out in? "None" would be a plausible answer, but I don't think it does that. It returns an empty string. Also plausible.

I was mainly confused with the wording but I think I got it now. Your right it should return the empty string, instead of none. Thanks.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
5
Views
2K
Replies
9
Views
3K
Replies
15
Views
2K
Replies
10
Views
3K
Replies
11
Views
1K
Replies
10
Views
2K
Replies
6
Views
3K
Back
Top