Python can multiply strings like so:
Python 3.4.3 (default, Mar 26 2015, 22:03:40)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 'my new text is this long'
>>> y = '#' * len(x)
>>> y
'########################'
>>>
Can Golang do the equivalent somehow?
In Python, the only operators that work with strings are addition and multiplication. You can't use strings if you're subtracting or dividing.
No, you can't. However you can use this function to repeat a character.
strings. Repeat() is a built-in function in Golang that is used to repeat a string for a specified number of times. It returns a new string which consists of a new count of copies of the given string.
In Go language, strings are different from other languages like Java, C++, Python, etc. it is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding.
It has a function instead of an operator, strings.Repeat
. Here's a port of your Python example, which you can run here:
package main
import (
"fmt"
"strings"
"unicode/utf8"
)
func main() {
x := "my new text is this long"
y := strings.Repeat("#", utf8.RuneCountInString(x))
fmt.Println(x)
fmt.Println(y)
}
Note that I've used utf8.RuneCountInString(x)
instead of len(x)
; the former counts "runes" (Unicode code points), while the latter counts bytes. In the case of "my new text is this long"
, the difference doesn't matter since all the characters are only one byte, but it's good to get into the habit of specifying what you mean:
len("ā") //=> 2
utf8.RuneCountInString("ā") //=> 1
Since this was a Python comparison question, note that in Python, the one function len
counts different things depending on what you call it on. In Python 2, it counted bytes on plain strings and runes on Unicode strings (u'...'
):
Python 2.7.18 (default, Aug 15 2020, 17:03:20)
>>> len('ā') #=> 2
>>> len(u'ā') #=> 1
Whereas in modern Python, plain strings are Unicode strings:
Python 3.9.6 (default, Jun 29 2021, 19:36:19)
>>> len('ā') #=> 1
If you want to count bytes, you need to encode the string into a bytearray
first:
>>> len('ā'.encode('UTF-8')) #=> 2
So Python has multiple types of string and one function to get their lengths; Go has only one kind of string, but you have to pick the length function that matches the semantics you want.
Yes, it can, although not with an operator but with a function in the standard library.
It would be very easy with a simple loop, but the standard library provides you a highly optimized version of it: strings.Repeat()
.
Your example:
x := "my new text is this long"
y := strings.Repeat("#", len(x))
fmt.Println(y)
Try it on the Go Playground.
Notes: len(x)
is the "bytes" length (number of bytes) of the string (in UTF-8 encoding, this is how Go stores strings in memory). If you want the number of characters (runes), use utf8.RuneCountInString()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With