How to change 000-000 to 000000 in Python? [duplicate] - python

This question already has answers here:
How to delete a character from a string using Python
(17 answers)
Closed 4 years ago.
I'm making an odoo10 module, but I need to change a string like
000-000
to
000000
How can I do that?

print("000-000".replace("-", "")) #Output: "000000"

Try this
Oldstr = “000-000”
Newstr = Oldstr.replace(“-”,””)
That should do it.
What I did:
Make the string “000-000”
Made a new string and replaced the “- ”in the old one with nothing and saved it in a new variable

Related

how to find many character in python without using OR? [duplicate]

This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed 1 year ago.
a = '/dvi/abcbbb'
if ('/dev/' in a) or ('/dv/' in a) or ('/dvi/' in a):
print(a)
/dvi/abcbbb
Can we do it without the OR statements in Python ?
You can reverse the check:
if os.path.dirname(a) in ["/dev", "/dv", "/dvi"]:
print(a)

String format: getting u'' inside the final string [duplicate]

This question already has answers here:
Removing u in list
(8 answers)
Closed 3 years ago.
I have a list of id's and I am trying the following below:
final = "ids: {}".format(tuple(id_list))
For some reason I am getting the following:
"ids: (u'213231231', u'weqewqqwe')
Could anyone help out on why the u is coming inside my final string. When I am trying the same in another environment, I get the output without the u''. Any specific reason for this?
Actually it is unicode strings in python
for literal value of string you can fist map with str
>>> final = "ids: {}".format(tuple(map(str, id_list)))
>>> final
"ids: ('213231231', 'weqewqqwe')

python padding formatting alternative? [duplicate]

This question already has answers here:
How can I fill out a Python string with spaces?
(14 answers)
Closed 5 years ago.
Is there a custom way of padding lines of text in python, I am using the escape characters "\t", but I wonder if there is an alternative.
for example
print('My Name is:')
print('Rambo')
print('Mambo')
Output:
.My Name is:
.....Rambo
..Mambo
Try using:
print('{:>15}'.format('My Name is:'))
Refer for examples:
PyFormat
Write a simple function for yourself.
def p(a,b):
print(" "*a + b)
p(1,"while")
This should return:" while"

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