How to get rid of "+" in a line? [duplicate] - python

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 3 years ago.
I have the txt file below and I want the remove the "+" if there is a + in front of the number.
+905061459318
+905458507534
+905437335094
I have tried almost all of the solutions that exist in Stackoverflow but still, I cannot remove the + from the line.
The code is here.
with open("numbers.txt") as f:
lines = [line.rstrip('\n') for line in open("numbers.txt")]
for line in lines:
if line.startswith("+"):
line.replace("+","")
else:
pass

You already have line.rstrip('\n'). Make that line.rstrip('\n').lstrip('+').

Related

Error encountered while changing case of the letter [duplicate]

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed last year.
I couldn't lowercase this desired line.I don't know what's the problem with this line?
"name1".lower()
print(name1)
You are not saving the lowercased string to a new variable. Try:
name1 = "name1".lower()
print(name1)

How to write a line starting and ending with ‘...’ in Python? [duplicate]

This question already has answers here:
Using quotation marks inside quotation marks
(12 answers)
Closed 3 years ago.
Say for example I want to print ‘I can fly’, including the quotations.
How to write the print statement?
you could do this:
print(" 'hi' ")
output: 'hi'

How to split a long python one-liner? [duplicate]

This question already has answers here:
Is it possible to break a long line to multiple lines in Python? [duplicate]
(7 answers)
Closed 5 years ago.
I have a long line that pylint is complaining is breaking pep8 E501
count_approvers = self.leave_not_required_user.company.leave_approvers.count()
How can I split this over 2 lines?
Configure your pylint for length of line. Or you can use '\' after '='
count_approvers = \
self.leave_not_required_user.company.leave_approvers.count()
You could use a temporary alias, if you may:
company = self.leave_not_required_user.company
count_approvers = company.leave_approvers.count()

Why is this split method not working correctly? [duplicate]

This question already has answers here:
why is python string split() not splitting
(3 answers)
Closed 6 years ago.
I'm trying to split
<team>
into just team, here is the code I'm using:
s = "<team>"
s.split(">")[1]
s
'<team>'
s.split(">")[1].split("<")[0]
s
'<team>
As you can see, it's still leaving me with
<team>
anyone know why>
str.split() function returns a list, it does not split the string in place.
You'll need to make a new variable:
s = "<team>"
t = s.split(">")[1]
t

Remove "x" number of characters from a string? [duplicate]

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Understanding slicing
(38 answers)
Closed 8 years ago.
In Python: How do I write a function that would remove "x" number of characters from the beginning of a string?
For instance if my string was "gorilla" and I want to be able remove two letters it would then return "rilla".
OR if my string was "table" and I wanted to remove the first three letters it would return "le".
Please help and thank you everyone!
You can use this syntax called slices
s = 'gorilla'
s[2:]
will return
'rilla'
see also Explain Python's slice notation

Categories

Resources