str « Format « Python Data Type Q&A

Home
Python Data Type Q&A
1.array
2.date
3.dictionary
4.Format
5.integer
6.List
7.numpy
8.regex
9.string
10.tuple
Python Data Type Q&A » Format » str 

1. str.format(list) with negative index doesn't work in Python    stackoverflow.com

I use a negative index in replacement fields to output a formatted list,but it raises a TypeError.The codes are as follows:

>>> a=[1,2,3]
>>> a[2]
3
>>> a[-1]
3
>>> 'The last:{0[2]}'.format(a)
'The last:3'
>>> 'The last:{0[-1]}'.format(a)
Traceback (most recent ...

2. Python 2.6+ str.format() and regular expressions    stackoverflow.com

Using str.format() is the new standard for formatting strings in Python 2.6, and Python 3. I've run into an issue when using str.format() with regular expressions. I've written a regular expression to ...

3. Python: How do you format a string % number with str.format()    stackoverflow.com

How can I format a string like this

"%01.2f" % pr"%01.2f" % some_number
but using the str.format() style? I'm used to this:
'{0} {1}'.format(somevalue1, somevalue2)
Is there a way to use that function style vs ...

4. str.format() does not work, keyError    stackoverflow.com

The following code raise a keyError exception:

addr_list_formatted = []
addr_list_idx = 0
    for addr in addr_list: # addr_list is a list
        addr_list_idx ...

5. How to represent a ListProperty(str) property with string formatting?    stackoverflow.com

I have a ListProperty(str):

class Item(db.Model):
    tags = db.ListProperty(str)
Sometimes I need to edit the tags already written to datastore, so I created an edit page:
class EditItem(webapp.RequestHandler):
    ...

6. Python3.2 Str.format value repetition    stackoverflow.com

I'm generating specifically formated input files for a program, and using a small python tkinter GUI frontend to do so. The old code made use of fortran format statements. Unless there's ...

7. How to use str.format() in python?    stackoverflow.com

What is wrong in this piece of code?

dic = { 'fruit': 'apple', 'place':'table' }
test = "I have one {fruit} on the {place}.".format(dic)
print(test)

>>> KeyError: 'fruit'

8. Python 3.2: How to pass a dictionary into str.format()    stackoverflow.com

I've been reading the Python 3.2 docs about string formatting but it hasn't really helped me with this particular problem. Here is what I'm trying to do:

stats = { 'copied': 5, 'skipped': ...

9. struct.error: unpack str size does not match format    python-forum.org

Hi, I came up with the following error while my number is big. I don't know what format should I use. It is giving error on the following line: properties = unpack( '%ds' % size, devcmds[index_num : (index_num + size)] )[0] The values are as following when it gives error: index_num = 12 size = 66496L (index_num + size) = 66508L ...

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.