- 1
Raising a string to a power?
Hey everyone, I was wondering if it's possible to raise a string to a power. I've tried spam**2 in the console but have ended up with this error Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' Any tips?
4 ответов
+ 3
No, you cannot raise a string by a number but you can:
- add strings: "abc" + "def" results in "abcdef"
- multiply string by a number: "abc" * 3 gives "abcabcabc"
Note: From the definiton of the multiplication above power does not make sense as it would require multiplication of a string by a string to be defined.
+ 1
I do not think that is possible. Also, what result would you expect from raising a string to a power? :)
+ 1
That is not possible
0
Well, if you expected to print spam twice by giving that a shot.... the correct way is to use loops :)