Determine if the string is palindrome [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
This function takes a string as input and returns True if string is palindrome and
False otherwise. A palindrome is a symmetric sequence of characters, reading
the same forward and backward.
For example: radar, anna, mom, dad, …

You can do this like the below:
def is_palindrome(phrase):
string = ""
for char in phrase:
if char.isalnum():
string += char
print(string)
return string[::-1].casefold() == string.casefold()
word = input("Please enter a word to check: ")
if is_palindrome(word):
print("'{}' is a palindrome".format(word))
else:
print("'{}' is not a palindrome".format(word))

Related

Replacing a string's character with a letter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 days ago.
Improve this question
I'm coding hangman and the problem I'm facing is that i cant fill in the blanks of the word with the correct location with the letters.
I want to be able to type in a letter and it'll fill in the blank with that letter at the correct location.
code block:
#converts selected word into a array
hidden_word = input('Enter a word ')
List = [m[0] for m in hidden_word]
x = len(hidden_word)
d = [(('_')*x)]
d = "".join(d)
print(d)
#fills in gaps with letter you've guessed
first_guess = input('Guess a letter ')
if first_guess in List:
y = str.find(hidden_word, first_guess)
count = List.count(first_guess)
f = d.replace(d[y],first_guess,count)
print(f)
print('YESS')
else:
print('NOO')

Can someone explain this Python code to me? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
def spin_words(sentence):
# Your code goes here
return " ".join([x[::-1] if len(x) >= 5 else x for x in sentence.split(" ")])
I don't understand this one.
sentence.split(" ") gets all the words in the sentence input parameter by separating when it finds a whitespace.
for x in sentence.split(" ") applies the loop to each word resulting from the previous statement
x[::-1] if len(x) >= 5 else x reverses the word if its length is greater than 5
return " ".join returns the result of the previous statement, joining the words with whitespaces

Sort the sequence alphabetically-Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to write a python function that accepts a hyphen separated sequence of colors as input and returns the colors in a hyphen separated sequence after sorting them alphabetically
constraint: All colors will be completely in either lower case or upper case
sample input: green-red-yellow-black-white
sample output: black-green-red-white-yellow
My code -
my_str = input("Enter a string: ")
words = [word.lower() for word in my_str.split()]
words.sort()
print("The sorted words are:")
for word in words:
print(word)
This works -
def str_sort(string):
b = string.split('-')
b.sort()
c = '-'.join(b)
return c
a = input('Enter a sequence of colors separated with hyphen: ')
str_sort(a)
This will solve your problem:
inputs = "green-red-yellow-black-white"
def sort_inputs(inputs):
inputs_list = inputs.split('-')
sorted_input = sorted(inputs_list)
final = "-".join(term for term in sorted_input)
return final
final = sort_inputs(inputs)
print(final)

How to move the last letter of a string to the front? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm doing the challenge in groklearning but I can't seem to figure out how to do this.
My program needs to do a few things:
Check if word ends in ay, if it does:
Remove the ay from the end of the word
Move the (now) last letter to the front of the word.
Print out the new word.
I don't know how to move the letter to the front. This is my code so far:
word = input('Word: ')
if 'ay' in word:
word1 = word[-1] + word[:-1]
word2 = word1[2:]
word3 = word2[-1] + word2[:-1]
print(word3)
else:
print(word)
If I were to type in 'athsmay' for the input, it sends out 'athsm'. Thanks in advance!
You can explicitly test that the word ends with 'ay', and do the concatenation in one line:
>>> def rearrange(word):
... if word.endswith('ay'):
... word = word[-3] + word[:-3]
... return word
...
>>> rearrange('athsmay')
'maths'
Based on the rules you gave:
Pop off 'ay' from the end, giving 'athsm'
Move the last letter of that result to the front
word[-3] uses negative indexing, where word[-1] is the last letter in the word. word[:-3] is the slice up to but not including that letter.

How to replace single word in string not replacing other with same chars [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this code:
s = 'letter of letters'
for i in s.split(" "):
if len(i) > 4:
s = s.replace(i, i[::-1])
print s
But this prints "rettel of rettels" which means code replace all 'letter' in string, but i need to replace only by every word themself.
How it possible to chagne single word but not all in string? I need 'rettel of srettel'
You need to collect all the modified words separately, instead of modifying the string in your loop:
words = s.split(' ')
results = []
for word in words:
if len(word) > 4:
results.append(word[::-1])
else:
results.append(word)
print(' '.join(results))

Categories

Resources