Changing words on a sentence to special characters
I am trying to change some words in a sentence to special characters but I don't get the required output. Also i have tried using the replace method which doesn't seen to replace everything but only the first word.
new_sentence = ''
sentence = input('Enter your word:')
for char in sentence:
if 'the' in sentence:
new_sentence += '~'
elif 'as' in sentence:
new_sentence += '^'
elif 'and' in sentence:
new_sentence += '+'
elif 'that' in sentence:
new_sentence += '$'
elif 'must' in sentence:
new_sentence += '&'
elif 'Well those' in sentence:
new_sentence += '% #'
else:
new_sentence += sentence
print(new_sentence)
This is what happens when i run it.
Enter your word:the as much and
~~~~~~~~~~~~~~~
python
add a comment |
I am trying to change some words in a sentence to special characters but I don't get the required output. Also i have tried using the replace method which doesn't seen to replace everything but only the first word.
new_sentence = ''
sentence = input('Enter your word:')
for char in sentence:
if 'the' in sentence:
new_sentence += '~'
elif 'as' in sentence:
new_sentence += '^'
elif 'and' in sentence:
new_sentence += '+'
elif 'that' in sentence:
new_sentence += '$'
elif 'must' in sentence:
new_sentence += '&'
elif 'Well those' in sentence:
new_sentence += '% #'
else:
new_sentence += sentence
print(new_sentence)
This is what happens when i run it.
Enter your word:the as much and
~~~~~~~~~~~~~~~
python
Are you sure you did the replace method right?
– exe
Nov 28 '18 at 17:05
If your sentence contains "The", then only your first conditional will trigger, and the rest won't, but it will still loop over all of the characters in the input string and trigger your first condition for each character
– G. Anderson
Nov 28 '18 at 17:06
First, you're looping through the chars, what seems weird, as it'll loop through each individual letters, so t, h, e, etc... If you'd like to loop through all words, first usesentence.strip()
in order to generate a list with all the words, so just usefor word in sentence.strip()
for having the results you want, @daliseiy
– Luan Naufal
Nov 28 '18 at 17:10
You would wantsentence.split()
not.strip()
, but that still would be highly inefficient.
– rahlf23
Nov 28 '18 at 17:12
add a comment |
I am trying to change some words in a sentence to special characters but I don't get the required output. Also i have tried using the replace method which doesn't seen to replace everything but only the first word.
new_sentence = ''
sentence = input('Enter your word:')
for char in sentence:
if 'the' in sentence:
new_sentence += '~'
elif 'as' in sentence:
new_sentence += '^'
elif 'and' in sentence:
new_sentence += '+'
elif 'that' in sentence:
new_sentence += '$'
elif 'must' in sentence:
new_sentence += '&'
elif 'Well those' in sentence:
new_sentence += '% #'
else:
new_sentence += sentence
print(new_sentence)
This is what happens when i run it.
Enter your word:the as much and
~~~~~~~~~~~~~~~
python
I am trying to change some words in a sentence to special characters but I don't get the required output. Also i have tried using the replace method which doesn't seen to replace everything but only the first word.
new_sentence = ''
sentence = input('Enter your word:')
for char in sentence:
if 'the' in sentence:
new_sentence += '~'
elif 'as' in sentence:
new_sentence += '^'
elif 'and' in sentence:
new_sentence += '+'
elif 'that' in sentence:
new_sentence += '$'
elif 'must' in sentence:
new_sentence += '&'
elif 'Well those' in sentence:
new_sentence += '% #'
else:
new_sentence += sentence
print(new_sentence)
This is what happens when i run it.
Enter your word:the as much and
~~~~~~~~~~~~~~~
python
python
edited Nov 28 '18 at 17:03
Mark Meyer
39.7k33363
39.7k33363
asked Nov 28 '18 at 17:00
DaliseiyDaliseiy
143
143
Are you sure you did the replace method right?
– exe
Nov 28 '18 at 17:05
If your sentence contains "The", then only your first conditional will trigger, and the rest won't, but it will still loop over all of the characters in the input string and trigger your first condition for each character
– G. Anderson
Nov 28 '18 at 17:06
First, you're looping through the chars, what seems weird, as it'll loop through each individual letters, so t, h, e, etc... If you'd like to loop through all words, first usesentence.strip()
in order to generate a list with all the words, so just usefor word in sentence.strip()
for having the results you want, @daliseiy
– Luan Naufal
Nov 28 '18 at 17:10
You would wantsentence.split()
not.strip()
, but that still would be highly inefficient.
– rahlf23
Nov 28 '18 at 17:12
add a comment |
Are you sure you did the replace method right?
– exe
Nov 28 '18 at 17:05
If your sentence contains "The", then only your first conditional will trigger, and the rest won't, but it will still loop over all of the characters in the input string and trigger your first condition for each character
– G. Anderson
Nov 28 '18 at 17:06
First, you're looping through the chars, what seems weird, as it'll loop through each individual letters, so t, h, e, etc... If you'd like to loop through all words, first usesentence.strip()
in order to generate a list with all the words, so just usefor word in sentence.strip()
for having the results you want, @daliseiy
– Luan Naufal
Nov 28 '18 at 17:10
You would wantsentence.split()
not.strip()
, but that still would be highly inefficient.
– rahlf23
Nov 28 '18 at 17:12
Are you sure you did the replace method right?
– exe
Nov 28 '18 at 17:05
Are you sure you did the replace method right?
– exe
Nov 28 '18 at 17:05
If your sentence contains "The", then only your first conditional will trigger, and the rest won't, but it will still loop over all of the characters in the input string and trigger your first condition for each character
– G. Anderson
Nov 28 '18 at 17:06
If your sentence contains "The", then only your first conditional will trigger, and the rest won't, but it will still loop over all of the characters in the input string and trigger your first condition for each character
– G. Anderson
Nov 28 '18 at 17:06
First, you're looping through the chars, what seems weird, as it'll loop through each individual letters, so t, h, e, etc... If you'd like to loop through all words, first use
sentence.strip()
in order to generate a list with all the words, so just use for word in sentence.strip()
for having the results you want, @daliseiy– Luan Naufal
Nov 28 '18 at 17:10
First, you're looping through the chars, what seems weird, as it'll loop through each individual letters, so t, h, e, etc... If you'd like to loop through all words, first use
sentence.strip()
in order to generate a list with all the words, so just use for word in sentence.strip()
for having the results you want, @daliseiy– Luan Naufal
Nov 28 '18 at 17:10
You would want
sentence.split()
not .strip()
, but that still would be highly inefficient.– rahlf23
Nov 28 '18 at 17:12
You would want
sentence.split()
not .strip()
, but that still would be highly inefficient.– rahlf23
Nov 28 '18 at 17:12
add a comment |
2 Answers
2
active
oldest
votes
You could store your character modifications in a dictionary and then apply them using replace()
in a for loop, like so:
sentence = 'This is the sentence that I will modify with special characters and such'
modifiers = {'the': '~', 'as': '^', 'and': '+', 'that': '$', 'must': '&', 'Well those': '% #'}
for i, v in modifiers.items():
sentence = sentence.replace(i, v)
Returns:
This is ~ sentence $ I will modify with special characters + such
This works great. Thank you.
– Daliseiy
Nov 28 '18 at 17:40
add a comment |
@rahlf23 has the right method, but just in case you wanted to work with your current implementation:
If you split the sentence into individual words, then iterate over those and check what the word itself is, instead of checking each character in the input string and checking whether any of the words to replace exist in the string, you'll be on the right track
for word in sentence.split():
if word.lower() == 'the':
new_sentence += '~'
...
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53524544%2fchanging-words-on-a-sentence-to-special-characters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could store your character modifications in a dictionary and then apply them using replace()
in a for loop, like so:
sentence = 'This is the sentence that I will modify with special characters and such'
modifiers = {'the': '~', 'as': '^', 'and': '+', 'that': '$', 'must': '&', 'Well those': '% #'}
for i, v in modifiers.items():
sentence = sentence.replace(i, v)
Returns:
This is ~ sentence $ I will modify with special characters + such
This works great. Thank you.
– Daliseiy
Nov 28 '18 at 17:40
add a comment |
You could store your character modifications in a dictionary and then apply them using replace()
in a for loop, like so:
sentence = 'This is the sentence that I will modify with special characters and such'
modifiers = {'the': '~', 'as': '^', 'and': '+', 'that': '$', 'must': '&', 'Well those': '% #'}
for i, v in modifiers.items():
sentence = sentence.replace(i, v)
Returns:
This is ~ sentence $ I will modify with special characters + such
This works great. Thank you.
– Daliseiy
Nov 28 '18 at 17:40
add a comment |
You could store your character modifications in a dictionary and then apply them using replace()
in a for loop, like so:
sentence = 'This is the sentence that I will modify with special characters and such'
modifiers = {'the': '~', 'as': '^', 'and': '+', 'that': '$', 'must': '&', 'Well those': '% #'}
for i, v in modifiers.items():
sentence = sentence.replace(i, v)
Returns:
This is ~ sentence $ I will modify with special characters + such
You could store your character modifications in a dictionary and then apply them using replace()
in a for loop, like so:
sentence = 'This is the sentence that I will modify with special characters and such'
modifiers = {'the': '~', 'as': '^', 'and': '+', 'that': '$', 'must': '&', 'Well those': '% #'}
for i, v in modifiers.items():
sentence = sentence.replace(i, v)
Returns:
This is ~ sentence $ I will modify with special characters + such
answered Nov 28 '18 at 17:05
rahlf23rahlf23
5,3363731
5,3363731
This works great. Thank you.
– Daliseiy
Nov 28 '18 at 17:40
add a comment |
This works great. Thank you.
– Daliseiy
Nov 28 '18 at 17:40
This works great. Thank you.
– Daliseiy
Nov 28 '18 at 17:40
This works great. Thank you.
– Daliseiy
Nov 28 '18 at 17:40
add a comment |
@rahlf23 has the right method, but just in case you wanted to work with your current implementation:
If you split the sentence into individual words, then iterate over those and check what the word itself is, instead of checking each character in the input string and checking whether any of the words to replace exist in the string, you'll be on the right track
for word in sentence.split():
if word.lower() == 'the':
new_sentence += '~'
...
add a comment |
@rahlf23 has the right method, but just in case you wanted to work with your current implementation:
If you split the sentence into individual words, then iterate over those and check what the word itself is, instead of checking each character in the input string and checking whether any of the words to replace exist in the string, you'll be on the right track
for word in sentence.split():
if word.lower() == 'the':
new_sentence += '~'
...
add a comment |
@rahlf23 has the right method, but just in case you wanted to work with your current implementation:
If you split the sentence into individual words, then iterate over those and check what the word itself is, instead of checking each character in the input string and checking whether any of the words to replace exist in the string, you'll be on the right track
for word in sentence.split():
if word.lower() == 'the':
new_sentence += '~'
...
@rahlf23 has the right method, but just in case you wanted to work with your current implementation:
If you split the sentence into individual words, then iterate over those and check what the word itself is, instead of checking each character in the input string and checking whether any of the words to replace exist in the string, you'll be on the right track
for word in sentence.split():
if word.lower() == 'the':
new_sentence += '~'
...
answered Nov 28 '18 at 17:11
G. AndersonG. Anderson
1,8641411
1,8641411
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53524544%2fchanging-words-on-a-sentence-to-special-characters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you sure you did the replace method right?
– exe
Nov 28 '18 at 17:05
If your sentence contains "The", then only your first conditional will trigger, and the rest won't, but it will still loop over all of the characters in the input string and trigger your first condition for each character
– G. Anderson
Nov 28 '18 at 17:06
First, you're looping through the chars, what seems weird, as it'll loop through each individual letters, so t, h, e, etc... If you'd like to loop through all words, first use
sentence.strip()
in order to generate a list with all the words, so just usefor word in sentence.strip()
for having the results you want, @daliseiy– Luan Naufal
Nov 28 '18 at 17:10
You would want
sentence.split()
not.strip()
, but that still would be highly inefficient.– rahlf23
Nov 28 '18 at 17:12