Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace first character
#1
Really having a hard time figuring this out. What I'm trying to do is every time the user inputs text, it will modify and replace every leading character with a "#". For example the user types: Hello my name is bob, python would print "#ello #y #ame #s #ob. Every leading character replaced.

get_text = str(input("Type any sentence"))
modified = get_text.replace([1] , '#', 9999)


print (modified)
Reply
#2
input() returns string, so no need to convert to string.

You need to replace first letter of every word, not only the first letter of whole string. In order to do so one can split string into words, change first letter of every word and construct new string from new words.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(Sep-27-2019, 01:57 PM)perfringo Wrote: input() returns string, so no need to convert to string.

You need to replace first letter of every word, not only the first letter of whole string. In order to do so one can split string into words, change first letter of every word and construct new string from new words.

I modified, still getting errors, where am I overlooking?

split_words = get_text.split(' ')
replace = split_words.replace([0] , '#', 9999)
join_words = ' '.join(split_words)
modified = join_words
Reply
#4
You are pretty close - you need to iterate all words with for-loop and make (correct) replacement.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Sep-27-2019, 02:25 PM)perfringo Wrote: You are pretty close - you need to iterate all words with for-loop and make (correct) replacement.

I'm lost, I know I'm over looking something this morning. :[

split_words = get_text.split(' ')
for characters in split_words:
    split_words.replace([0], '###'))
Reply
#6
split_words is a list.
In line 3 you are using the replace function on the list, when you want to do it on each element of the list (you are likely getting an error as replace is not a list function). Also, look up the proper syntax and use of replace - you discard the result.
Reply
#7
I am on the phone right now but I had something like below in mind:

>>> sentence = "Spam, ham and eggs" 
>>> print(" ".join(["".join(["#", word[1:]]) for word in sentence.split()]))
#pam, #am #nd #ggs
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
An easy step by step approach:
get_text = input("Type something ")
words = get_text.split(' ')
new_text = []
for word in words:
    new_text.append(word.replace(word[0],'#'))
result = ' '.join(new_text)
print(result)
Line 1 - get the phrase
Line 2 - Split the phrase at the spaces, creates a list "words"
Line 3 - create the new list (empty) for the modified words
Line 4 - start loop iterating through the words list
Line 5 - replace the first letter of each word with a # and append it to the list of modified words
Line 6 - join the modified words together to make a new result
Line 7 - print the result.

There are lots of ways to combine functions to reduce the number of lines, as perfringo showed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] unexpected character after line continuation character paul18fr 4 3,378 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,149 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Regex won't replace character with line break Tomf96 2 2,540 Jan-12-2020, 12:14 PM
Last Post: Tomf96
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,720 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Replace changing string including uppercase character with lowercase character silfer 11 6,124 Mar-25-2019, 12:54 PM
Last Post: silfer
  Search & Replace - Newlines Added After Replace dj99 3 3,387 Jul-22-2018, 01:42 PM
Last Post: buran
  SyntaxError: unexpected character after line continuation character Saka 2 18,537 Sep-26-2017, 09:34 AM
Last Post: Saka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020