Print All String Prefixes in Python

thumbnail

In this tutorial, you will learn to print all prefixes from a given string str. Prefixes means a prefix of str is a substring but also a leading part of str. The example is explained below.

Example:
Input: “inpows”
Output:
“i”
“in”
“inp”
“inpo”
“inpow”
“inpows”

The source code is show below.

def printPrefixes(str):
  prefix = ""
  for i in range(0, len(str)):
    prefix = prefix + str[i]
    print(prefix)

printPrefixes("inpows")
i
in
inp
inpo
inpow
inpows
Back To Top