Quick Question about Python concatenation

  • Python
  • Thread starter NATURE.M
  • Start date
  • Tags
    Python
In summary, the question involves concatenating two strings, 'movies' and 'goto', n times. If n is negative, it is treated as 0 and returns an empty string.
  • #1
NATURE.M
301
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
  • #2
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.
 
  • #3
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.
 

FAQ: Quick Question about Python concatenation

1. What is Python concatenation?

Python concatenation refers to the process of combining two or more strings together to create a single string. It is done using the "+" operator or the "%s" formatting operator.

2. How do I concatenate strings in Python?

To concatenate strings in Python, you can use the "+" operator or the "%s" formatting operator. Example: "Hello" + "World" will result in "HelloWorld".

3. Can I concatenate strings with other data types in Python?

Yes, you can concatenate strings with other data types in Python. However, you may need to convert the other data types to strings using the str() function before concatenation.

4. What is the difference between concatenation and interpolation in Python?

Concatenation in Python is the process of combining strings together using the "+" operator or the "%s" formatting operator. Interpolation, on the other hand, is the process of inserting values into a string using the "%s" formatting operator. In interpolation, the values are automatically converted to strings before being inserted into the string.

5. Can I concatenate multiple strings at once in Python?

Yes, you can concatenate multiple strings at once in Python. You can use the "+" operator to combine two or more strings together, or you can use the "%s" formatting operator to insert multiple values into a single string.

Similar threads

Replies
5
Views
495
Replies
9
Views
2K
Replies
15
Views
2K
Replies
10
Views
2K
Replies
5
Views
4K
Replies
11
Views
998
Replies
10
Views
1K
Replies
6
Views
3K
Replies
3
Views
634
Back
Top