How to replace single word in string not replacing other with same chars [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 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))

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

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)

Categories

Resources