"starts with" does not work (python regex) [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have this string:
m = 'Film/6702-BRINGING-UP-BABY" da'
and the following regax, that tries to find words that start with 'd':
movie = re.findall(r'^d.*', m)
print(movie)
the result is:
[]
and I can not understand why - there is a match in the string('da'), so why wont it find it?
I an working with pycharm, python 3.6

Using \b as boundary character we can find the words starting with specific character
import re
m = 'Film/6702-BRINGING-UP-BABY" da'
movie = re.findall(r'\bd.*', m)
print(movie)
output
['da']

Related

Python 3 str.format with decimal place [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
def ShowData(data = (x)):
for r in data:
print (“{:}, {:.2f}”.format(x, x))
keep getting this error:
File "<ipython-input-10-9963f96f39ca>", line 8
print (“{:}, {:.2f}”.format(x, x))
^
SyntaxError: invalid character in identifier
Replace the “ charachter with this " ?
# data is a list of lists
def ShowData(data = (x)):
for r in data:
print ("{:}, {:.2f}".format(x, x))

Getting two digits from a string in python with regex [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have multiple strings like this 90'4 I want to extract the digits from the string and sum them up to get 94.
I tried compiling the pattern.
pattern="\d'\d"
re.compile(pattern)
I tried the methods findall and match, but did not get what I wanted.
I need to use regex I cannot use .split()
Use \d+ with findall to extract numbers and then find their sum:
import re
s = "this is 90'4"
numbers = re.findall(r'\d+', s)
print(sum(map(int, numbers)))
# 94

New line (/n) not working in Python 2 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I tried to make a power calculator program in Python 2.7. It worked but I tried to write the values into a file and the /n didn't work. Here is the program:
import math
file = open("numbers.txt" , 'w')
c = 0
a = int(raw_input("A number: "))
b = int(raw_input("To the power "))
h = range(b)
h.append(b)
print 1
file.write('1')
for c in range(b):
print int((math.pow(a, h[c+1])))
k = (int((math.pow(a, h[c+1]))))
file.write((str(k)+"/n") `
You're using a normal slash (/). But you need to use a Backslash (\).
So, \n will add a new line.
\ is used for escape sequence.

Python If(and) statement syntax error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to make a simple console game in Python 2.7, where the user gets a dilemma and makes a decision by typing the number correlating to the option he wants to choose. This question is the main menu.
s = raw_input("Enter a number in the range 1 to %s\n> " % v
if (is_number(s) and s in q):
return s
I'm getting a "SyntaxError: Invalid syntax" from the if (is_number(s) and s in q) statemet when trying to run the program. It was working fine before i added the question.
This is my first real program.
You're missing a parentheses on the previous line:
s = raw_input("Enter a number in the range 1 to %s\n> " % v
# here--^

Sort each string inside list - Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to sort each string inside a list. For example, if I have the list ['bca', 'fed'] then I want my function to return ['abc', 'def'].
Currently trying to do this like so:
lines = [''.join.(sorted(e)) for e in lines]
What's wrong with this syntax?
Just remove the redundant dot (.):
lines = [''.join(sorted(e)) for e in lines]

Categories

Resources