Removing Python characters from a list [duplicate] - python

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.']

Related

Create a long list of characters (not numbers) with list comprehension [duplicate]

This question already has answers here:
Python: Short way of creating a sequential list with a prefix
(3 answers)
Create a list of strings with consecutive numbers appended
(6 answers)
Appending the same string to a list of strings in Python
(12 answers)
Closed 7 months ago.
In Python how can I need to create a long list that I'm trying to avoid typing, the list looks lis this
brands = ['_1','_2','_3'... '_998']
I can create the list of numbers with a for loop, but I'm trying to use list comprehension for the characters which should be faster.
Thanks!
my list=["_" + str(i) for i in range(1,999)]

Insert a '*' between each two characters in a string in Python [duplicate]

This question already has answers here:
Efficient way to add spaces between characters in a string
(5 answers)
Closed 2 years ago.
I need to insert a '*' between each two characters in a string in Python, for example, for 'ABCDE' I need to get 'A*B*C*D*E'. What's a Python way of doing it?
This could be a solution
string ='ABCDE'
x = '*'
print(x.join(string))

How to convert string without delimiter to list in python [duplicate]

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 3 years ago.
So I have string like this:
"UFFKTEWKW"
And I need to convert it to a list or tuple like this:
("UFF", "KTE", "WKW")
So every 3 letters from the string goes to separate element of list or tuple.
I can't use split() here because string doesn't have any delimiters. I don't wanna make some dummy for cycle for it. And I think there should be simple solution for it.
You can use this.
a="UFFKTEWKW"
out=tuple(a[i:i+3] for i in range(0,len(a),3))
# ('UFF', 'KTE', 'WKW')

PYTHON nex string splitting [duplicate]

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 7 years ago.
Hello I am learning PYTHON and was learning about string splitting .so I have a hex string named
A="aca80202"
I want to convert it into a list of 2 characters
B=["ac","a8","02",02"]
is there any way to do it?
If you are starting with Python you could do it in a simple way using a for loop:
A="aca80202"
B = []
for i in range(0, len(A), 2):
B.append(A[i:i+2])
EDIT: you could see more ways to do it here.

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