create a string of variable lenght

Günther Dietrich gd_usenet at spamfence.net
Sun Jan 31 07:46:16 EST 2010


Tracubik <affdfsdfdsfsd at b.com> wrote:

>i want to print on linux console (terminal) a message like this one:
>
>********************************
>error message of variable lenght
>********************************
>
>to print the asterisks line i do this:
>
>def StringOfAsterisks(myString):
>    asterisksString = "*"
>    for i in range(1,len(myString):
>       asterisksString += "*"
>    print asterisksString

def StringOfAsterisks(myString):
    print(''.ljust(len(myString), '*'))

You might use string methods .rjust() or .center() instead of method 
.ljust().


>the code seem to work for me, but it doesn't work properly if in errorMsg 
>there is some "esotic" char like euro char (€):
>
>>>> euro = "€"
>>>> len(euro)
>3

Maybe you might solve this if you decode your string to unicode.
Example:

|>>> euro = "€"
|>>> len(euro)
|3
|>>> u_euro = euro.decode('utf_8')
|>>> len(u_euro)
|1

Adapt the encoding ('utf_8' in my example) to whatever you use.

Or create the unicode string directly:

|>>> u_euro = u'€'
|>>> len(u_euro)
|1



Best regards,

Günther



More information about the Python-list mailing list