How would I make a word print backwards in python? [duplicate] - python

This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 5 years ago.
How would I make a word print backwards in Python? For example, if I have this:
word = input("Enter a word: ")
how would I make that print backwards?

The simplest one, using extended slice :
>>> word[::-1]
#driver values :
IN : word = 'abcd'
OUT : 'dcba'

Try This:
a=input()
print ("".join(reversed(a)))

Related

how to add several patterns to check if one of them is present into a python string? [duplicate]

This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed 2 years ago.
I know how to check if a string contain a particular pattern ex :
my_list=['hello.1', 'Holla',"Bonjour+","Saloute"]
for i in my_list:
if '.1' in i:
print(i)
hello.1
but how to add several patterns? for instance :
for i in my_list:
if '.1' or '+' in i:
print(i)
hello.1
Bonjour+
You can use any here.
liste=['hello.1', 'Holla', 'Bonjour+', 'Saloute']
for word in liste:
if any(i in word for i in ('.1','+')):
print(word)
hello.1
Bonjour+
Or
You can write
if '.1' in word or '+' in word

How to display all letters in a string except the first [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 3 years ago.
I want to display an input() string and want to display it so it prints all the letters of the string except the first letter.
string1 = input("enter first string")
string2 = input("enter second string")
print(string1[1] + (string2 - [0]))
I expected it to display as (for examples if string1 was pizza and string 2 was a salad) "palad"
You can use substring. like this:
string1 = input("enter first string")
string2 = input("enter second string")
print(string1[0] + string2[1:])
string2[1:] means: substring from character 2 till the end.

check strings of a list [duplicate]

This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed 4 years ago.
I want to when I input a string, if words in string is contain in a list of words, delete words in the strings
Or, you could do the following:
raw_string = input("Enter String:")
useless_list = ["Birds", "Cat"]
print(' '.join([i for i in raw_string.split() if i not in useless_list]))

Split the string into words [duplicate]

This question already has answers here:
Split string every nth character?
(19 answers)
Closed 4 years ago.
how to split a string into words of 2 letters. Like given string is "HelloThere" now i want to make it ["He","ll","oT","he","re"]. Please help to code that in python.
yourList = []
yourString = "HelloThere"
while yourString:
yourList.append(yourString[:2])
yourString = yourString[2:]
If you print yourList, you will get the result.

Capitalize first letter ONLY of a string in Python [duplicate]

This question already has answers here:
Capitalize a string
(9 answers)
How can I capitalize the first letter of each word in a string?
(23 answers)
Closed 6 years ago.
I'm trying to write a single line statement that assigns the value of a string to a variable after having ONLY the first letter capitalized, and all other letters left unchanged.
Example, if the string being used were:
myString = 'tHatTimeIAteMyPANTS'
Then the statement should result in another variable such as myString2 equal to:
myString2 = 'THatTimeIAteMyPANTS'
Like this:
myString= myString[:1].upper() + myString[1:]
print myString
Like Barmar said, you can just capitalize the first character and concatenate it with the remainder of the string.
myString = 'tHatTimeIAteMyPANTS'
newString = "%s%s" % (myString[0].upper(), myString[1:])
print(newString) # THatTimeIAteMyPANTS

Categories

Resources