This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 3 years ago.
I need a simple and effective algorithm to reverse a string (for example: WelCome to emoCleW ).
I tried a loop:
s=input("Enter String To Be Reversed:")
for i in range(len(s)+1,-1,-1):
print(str1[0:i])
but this didn't work for me
Like this:
s=input("Enter String To Be Reversed:")
print(s[::-1])
Related
This question already has answers here:
How to change a string into uppercase?
(8 answers)
Closed 2 years ago.
What is the best pythonic way to covert, a string as '11-2020' to 'NOV-2020' in Python.
I tried below code :
print(datetime.datetime.strptime(date, '%m-%Y').strftime('%b-%Y'))
But getting output like : Nov-2020 (I want Nov to be in caps)
print(datetime.datetime.strptime(date, '%m-%Y').strftime('%b-%Y').upper())
#NOV-2020
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:
Slicing strings in str.format
(6 answers)
Closed 6 years ago.
May be a question that has already been asked:
How can I print an integer in a user definied format.
For example
input = 123456
print(".....".format(input))
where the output should be:
"ab123.45.6"
Unfortunately, slicing in string formating is not possible. So something like:
"ab{0[:2]}.{0[3:4]}.{0[5]}".format(str(input))
would not work.
But is there a better solution than:
"ab{0[0]}{0[1]}{0[2]}.{0[3]}{0[4]}.{0[5]}".format(str(input))
Why not make the slices format() arguments? It looks a bit cleaner.
user_input = str(123456)
print("ab{0}.{1}.{2}".format(user_input[:3], user_input[3:5], user_input[5]))
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
How do I reverse each word in a string, but in the same sentence order? [duplicate]
(1 answer)
Closed 6 years ago.
How to reverse string in python? my input string is "Hey Gun" now i want to display "Gun Hey" as output. I have tried using slice operator like [::-1] but it won't shows proper output how it works in python?
Do splitting and then reversing and then joining.
' '.join(string.split()[::-1])
Using split and reversed (a bit slower though):
>>> a
'Hey Gun'
>>> ' '.join(reversed(a.split()))
'Gun Hey'
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