Search code examples
pythonstringletters

How do you get x amounts of y in one string and put it in another string in python?


If the string is "43 Lobsters and 3 Crabs" I want to get only the s's from this string. There are three s's so in my new string I must have just "sss".


Solution

  • why dont you use the count function?

    thestr = "43 Lobsters and 3 Crabs"
    y = 's'
    x = thestr.count(y)
    

    so now you know how many times it appears in the string use that variable to construct a string

    finstr = y * x