Tracubik wrote:

********************************
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

so i create the asterisksString of the lenght i need

There is a better way of creating asterisksString?

well, to make it more pythonic (ignoring camel-case variable-names for now), I'd just use

  print '*' * len(myString)
  print myString
  print '*' * len(myString)

possibly stashing the resulting asterisksString once:

  asterisks_string = '*' * len(my_string)
  print asterisks_string
  print my_string
  print asterisks_string

If I used it in multiple places, I might wrap it in a function and/or define a "DIVIDER_CHARACTER" constant, something like

  DIVIDER_CHARACTER = '*'
  def surround(s, divider=DIVIDER_CHARACTER):
    d = divider[0]
    print d * len(s)
    print s
    print d * len(s)

  surround('hello')
  surround('world', '-')
  surround('foo', 'xo')

depending on the sort of behavior you want

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

I suspect you're seeing the byte-representation of your string with a particular-yet-undisclosed encoding. If it was a unicode string (a good idea to specify the encoding at the top of your file), the length should be accurate, so what happens if you

  >>> euro = u"€"
  >>> len(euro)

? (I don't have ready access to a terminal where I can enter unicode characters, and you don't display the representation of the string with

  print repr(euro)

so I can steal the byte values to recreate it; but I suspect the result will correctly be 1).

-tkc






--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to