← Back to challenges

Star Shorthand

PythonHardstringsregexformattingloops

Instructions

Write a function that converts a string into star shorthand. If a character is repeated n times, convert it into character*n.

Examples

to_star_shorthand("abbccc") ➞ "ab*2c*3"

to_star_shorthand("77777geff") ➞ "7*5gef*2"

to_star_shorthand("abc") ➞ "abc"

to_star_shorthand("") ➞ ""

Notes

  • Leave lone occurrences of a character as is.
  • Return an empty string if given an empty string input.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.