Quick Question about Python concatenation

  • Context: Python 
  • Thread starter Thread starter NATURE.M
  • Start date Start date
  • Tags Tags
    Python
Click For Summary
SUMMARY

The discussion centers on Python string concatenation using the syntax `n * "movies goto"`, where `n` determines the number of repetitions. It is established that if `n` is negative or zero, the result is an empty string rather than None. Participants clarified that the behavior of the concatenation operation does not yield None, but instead returns an empty string, which aligns with Python's handling of string multiplication.

PREREQUISITES
  • Understanding of Python string operations
  • Familiarity with Python syntax for multiplication
  • Basic knowledge of control flow in programming
  • Experience with Python interpreters for testing code
NEXT STEPS
  • Explore Python string methods and their behaviors
  • Learn about Python's handling of negative integers in operations
  • Investigate the differences between returning None and an empty string in Python
  • Practice string manipulation in Python using various examples
USEFUL FOR

Python developers, students learning programming concepts, and anyone interested in understanding string operations in Python.

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.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
Replies
55
Views
7K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K