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)
Related
This question already has answers here:
Call methods of a number literal [duplicate]
(1 answer)
Accessing attributes on literals work on all types, but not `int`; why? [duplicate]
(4 answers)
Closed 1 year ago.
If I am doing this
>>> 5.bit_length()
I am getting SyntaxError: invalid syntax
but runs fine when using space
>>> 5 .bit_length()
Result -> 3
How does it reads the space ?
This question already has answers here:
Slice every string in list in Python
(3 answers)
What is the purpose of the two colons in this Python string-slicing statement?
(1 answer)
Closed 4 years ago.
If i had a list as an example:
a = ['Hello_1.txt', 'Hello_2.txt']
In Python is it possible to somehow remove the first 5 characters ('Hello')
and the last 3 characters ('txt') from each of the items in the list ?
You could use a list-comprehension and string slicing:
[s[5:-3] for s in a]
which gives what you describe (not sure this is the neatest output though!)
['_1.', '_2.']
This question already has answers here:
Why does += behave unexpectedly on lists?
(9 answers)
Why can't I add a tuple to a list with the '+' operator in Python?
(3 answers)
Closed 7 years ago.
I saw someone wrote an interesting python line online, but couldn't understand why it works. So we can try the following lines in python interpreter:
s=[1]
s=s+(1,-1)
This will result in an error "TypeError: can only concatenate list (not "tuple") to list". But if done in another way:
s=[1]
s+=(1,-1)
will result in s = [1,1,-1]
So I used to thought x=x+y is equivalent to x+=y, can someone tell me how they are different and why the second way works? Thanks in advance.
Instead of += use list.extend:
s = [1]
s.extend((1,-1))
This question already has answers here:
Create an array with same element repeated multiple times
(25 answers)
Closed 7 years ago.
print "*" * 80
This is an elegant way to print delimiters (that has 80 astreisk symbols) in python log files. How can i do this in nodejs?
In node.js it is something like this:
console.log(new Array(80).join('*'));
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