How to move the last letter of a string to the front? [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 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.

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')

How do I put characters inbetween characters [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 5 months ago.
Improve this question
word2 = input("put in the word you want me to repeat: ")
letter = ""
print("The original string is: " + word2)
for x in range(len(word2)):
letter += word2[x]
So if i put in "dwas" it will just print "dwas". How do I make it print "d-ww-aaa-ssss"?
You can use enumerate passing the string value from input, and the start value as 1, then repeat the characters n times, and finally join by -:
>>> print('-'.join(v*i for v,i in enumerate(inp,1)))
d-ww-aaa-ssss
By composiing built-in functions:
s = "hallo"
new_s = '-'.join(map(str.__mul__, s, range(1, len(s)+1)))
print(new_s)
#h-aa-lll-llll-ooooo
A for loop approach similar to the one in the question
s = "hallo"
# construct the output character per character
new_s = ''
# iterate over (index, character)-pairs, index start from 1
for i, char in enumerate(s, 1):
# add to output i-times the same character followed by -
new_s += f'{char*i}-'
# remove the last character (always the -)
new_s = new_s.rstrip('-')
# check result
print(new_s)

Determine if the string is palindrome [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 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))

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

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