Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Python printing format

Status
Not open for further replies.

tpkemme

Programmer
Joined
Oct 13, 2010
Messages
1
Location
US
I have a pretty simple coding function that returns all possible substrings of a string including the string itself

def main():
str = raw_input ("Enter first string: ")
size = len(str)
for sub_len in range (1, size + 1):
for idx in range (0, size - sub_len + 1):
sub_str = str[idx : idx + sub_len]
print sub_str

main()

the current output looks like this for the string 'abcd'
a
b
c
d
ab
bc
cd
abc
bcd
abcd

my question is, how can i get the program to print the substrings in the opposite order in terms of length? i.e.:

abcd
abc
bcd
ab
bc
cd
a
b
c
d
 
My quick hack of your code gives
Code:
    str=raw_input('Enter String?')
    size=len(str)+1
    for sublen in range(size,0,-1):
        for idx in range(size-sublen):
            print "string %s" %str[idx:idx+sublen]

seems to work

I do not Have A.D.D. im just easily, Hey look a Squirrel!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top