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))
Related
This question already has answers here:
Remove text between () and []
(8 answers)
Best way to replace multiple characters in a string?
(16 answers)
How to replace multiple substrings of a string?
(28 answers)
Closed last month.
I have string like this.
a="what is[hags] your name?"
I want delete [hags] from string!
output:
a="what is your name?"
This question already has answers here:
Count multiple letters in string Python
(4 answers)
Is it possible to use .count on multiple strings in one command for PYTHON?
(7 answers)
Closed 1 year ago.
New to Python.
Say I wanted to get the total number of '.' or '!' in a body of text, what is the 'pythonic' way of doing so? I'd think there would be something like: text.count(LIST)
I realize the below would work, but wondering what the python people think.
for char in list(text):
if char in LIST:
sum += 1
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:
How to write string literals in Python without having to escape them?
(6 answers)
Closed 6 years ago.
\201 is a character code recognised in Python. What is the best way to ignore this in strings?
s = '\2016'
s = s.replace('\\', '/')
print s #6
If you have a string literal with a backslash in it, you can escape the backslash:
s = '\\2016'
or you can use a "raw" string:
s = r'\2016'
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