Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6923557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:33:02+00:00 2026-05-27T10:33:02+00:00

I have this code (printing the occurrence of the all permutations in a string)

  • 0

I have this code (printing the occurrence of the all permutations in a string)

def splitter(str):
    for i in range(1, len(str)):
        start = str[0:i]
        end = str[i:]
        yield (start, end)
        for split in splitter(end):
            result = [start]
            result.extend(split)
            yield result    

el =[];

string = "abcd"
for b in splitter("abcd"):
    el.extend(b);

unique =  sorted(set(el));

for prefix in unique:
    if prefix != "":
        print "value  " , prefix  , "- num of occurrences =   " , string.count(str(prefix));

I want to print all the permutation occurrence there is in string varaible.

since the permutation aren’t in the same length i want to fix the width and print it in a nice not like this one:

value   a - num of occurrences =    1
value   ab - num of occurrences =    1
value   abc - num of occurrences =    1
value   b - num of occurrences =    1
value   bc - num of occurrences =    1
value   bcd - num of occurrences =    1
value   c - num of occurrences =    1
value   cd - num of occurrences =    1
value   d - num of occurrences =    1

How can I use format to do it?

I found these posts but it didn’t go well with alphanumeric strings:

python string formatting fixed width

Setting fixed length with python

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:33:03+00:00Added an answer on May 27, 2026 at 10:33 am

    EDIT 2013-12-11 – This answer is very old. It is still valid and correct, but people looking at this should prefer the new format syntax.

    You can use string formatting like this:

    >>> print '%5s' % 'aa'
       aa
    >>> print '%5s' % 'aaa'
      aaa
    >>> print '%5s' % 'aaaa'
     aaaa
    >>> print '%5s' % 'aaaaa'
    aaaaa
    

    Basically:

    • the % character informs python it will have to substitute something to a token
    • the s character informs python the token will be a string
    • the 5 (or whatever number you wish) informs python to pad the string with spaces up to 5 characters.

    In your specific case a possible implementation could look like:

    >>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
    >>> for item in dict_.items():
    ...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
    ... 
    value   a - num of occurances = 1
    value  ab - num of occurances = 1
    value abc - num of occurances = 1
    

    SIDE NOTE: Just wondered if you are aware of the existence of the itertools module. For example you could obtain a list of all your combinations in one line with:

    >>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
    ['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']
    

    and you could get the number of occurrences by using combinations in conjunction with count().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code in QT and all i want to to catch the
I have this Perl code that is only printing the first line instead of
I have this jquery code i would like it printing on a new line
I have this following test code: public static final String[] list = { apple,ball,cat,dog,egg,fan,girl,hat,igloo,jerk
this code creates a csv file. However, I have avoided printing out commas in
I currently have this code use for printing a table in mysql database ,
I have code written that converts my Document to a string prior to printing
I have this code in jQuery, that I want to reimplement with the prototype
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I have this code that performs an ajax call and loads the results into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.