Remove Python Last String

Asked 2 years ago, Updated 2 years ago, 40 views

ss = 'python'

for i in range(0,6) :

print(ss[i]+'$',end='')

I want to add $ in the middle of the string and make sure there is no $ in the end Is there a way to remove the last $

python string

2022-09-21 17:14

3 Answers

'$'.join(list(str))


2022-09-21 17:14

import itertools as it

ss = 'python'

# The way digda posted it
'$'.join(it.repeat(ss, 5))
'python$python$python$python$python'

# You can just do * 5.
'{}$'.format(ss) * 5
'python$python$python$python$python$'

# Remove last character $
('{}$'.format(ss) * 5)[0:-1]
'python$python$python$python$python'


2022-09-21 17:14

*Extra length

Try it in GO language

https://play.golang.org/p/m8hOxlE3Mkb

package main

import (
    "fmt"
    "strings"
)

func main() {
    ss := "python"
    s1 := strings.Repeat(ss+"$", 5) // strings.Repeat(fmt.Sprintf("%s$", ss), 5)
    fmt.Println(s1[0 : len(s1)-1])  // python$python$python$python$python

    L := []string{}
    for i := 0; i < 5; i++ {
        L = append(L, ss)
    }
    s2 := strings.Join(L[:], "$")
    fmt.Println(s2) // python$python$python$python$python
}


2022-09-21 17:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.