Python bug, or am I misunderstanding something?

In summary, the conversation is about a person who is trying to code in Python for fun but has encountered an obstacle. They are unsure if it is a bug in their outdated Python version or their own misunderstanding. The person also discusses an issue with accessing arguments using [] and mentions being unable to upgrade to the newest version of Python. They then receive a suggestion to use m.group() instead of m.group(0), which resolves the issue.
  • #1
Borek
Mentor
28,953
4,252
I don't know python, yet I am trying to code something, just for fun. But I have hit some strange obstacle and I don't know if it is a bug in my Python version (dated, it is over a year old, under Raspbian), or is it me misunderstanding something?

Code:
pattern = re.compile('\[(\d+)\]\[(\d+)\](.*)')
line = '[123][456]789'
print 'original string:'
print line
m = pattern.match(line)
print 'matched substrings:'
print m.groups()
fst = '[{}][{}]{}'
print 'output using strings returned from m.group():'
print fst.format(m.group(0), m.group(1), m.group(2))
print 'output using just strings:'
print fst.format('123','456','789')

Output:

Code:
original string:
[123][456]789
matched substrings:
('123', '456', '789')
output using strings returned from m.group():
[[123][456]789][123]456
output using just strings:
[123][456]789

Why is the output using m.group() strings garbled? Strings are matched correctly, m.group() returns a string, so it should work the same way it works in the last output line - but it doesn't. I understand [] can be used to access arguments’ items, but I don't see a way of escaping them, besides, it doesn't seem to be a problem in other cases.

The simplest thing to try would be to upgrade to the newest python version, but I am unable to. I get message that some files are missing and suggestion to do full update - which I don't want to try, as I was not able to setup my WiFi dongle on another Pi with the newest Raspbian.
 
Technology news on Phys.org
  • #2
Try:
Code:
print fst.format(m.group(1), m.group(2), m.group(3))

m.group(0) returns the entire string '[123][456]789'.
 
  • Like
Likes 1 person
  • #3
Thanks. Much better now.

Will see if that's the end of the story :wink:
 

Related to Python bug, or am I misunderstanding something?

1. What is a Python bug?

A Python bug is an error or flaw in the code of a Python program. It can cause unexpected behavior or crashes, and can be caused by a variety of factors such as incorrect syntax, logical errors, or compatibility issues.

2. How can I determine if I have encountered a Python bug?

If you are experiencing unexpected behavior in your Python program, it is possible that you have encountered a bug. You can try debugging your code, checking for any syntax errors, and consulting the official Python documentation for known bugs and solutions.

3. Is it possible that the issue I am experiencing is due to a misunderstanding rather than a Python bug?

Yes, it is possible that the issue you are experiencing is due to a misunderstanding of the Python language or a specific function or module. It is always important to carefully review your code and consult resources such as documentation or online forums to ensure that you are using Python correctly.

4. How can I report a Python bug?

If you believe you have encountered a bug in Python, you can report it to the official Python bug tracker. Before reporting a bug, it is recommended to search the tracker to see if the bug has already been reported. When reporting a bug, provide as much detail as possible, including the error message, steps to reproduce the bug, and your operating system and Python version.

5. How can I prevent encountering Python bugs in the future?

To prevent encountering bugs in Python, it is important to regularly update your Python installation to the latest version, as new updates often include bug fixes. Additionally, following best practices for coding and thoroughly testing your code can help catch and prevent bugs before they occur.

Similar threads

  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
3
Views
335
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
3
Views
904
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top