I want to write a function which checks whether a letter is part of a word e.g
"u" part of word in "i c u" = false
"u" part of word in "umbrella" = true
asked Apr 15, 2013 at 6:53
1
>>> text = "i c u"
>>> letter = "u"
>>> any[letter in word and len[word] > 1 for word in text.split[]]
False
>>> text = "umbrella"
>>> any[letter in word and len[word] > 1 for word in text.split[]]
True
You might change letter in word
to letter.lower[] in word.lower[]
depending if you are case sensitive or nt.
answered Apr 15, 2013 at 6:55
jamylakjamylak
124k29 gold badges227 silver badges227 bronze badges
3
Assuming you mean 'in a word' to be 'has at least one character on either side that is a "word character"', this would work:
import re
def letter_in_a_word[letter, words]:
return bool[re.search[ur'\w{0}|{0}\w'.format[letter], words]]
letter_in_a_word['u', 'i c u'] # False
letter_in_a_word['u', 'umbrella'] # True
letter_in_a_word['u', 'jump'] # True
answered Apr 15, 2013 at 7:02
Tim HeapTim Heap
1,66111 silver badges11 bronze badges
>>> word = 'i c u'
>>> letter = 'u'
>>> letter in word.split[' ']
True
>>> word = 'umbrella'
>>> letter in word.split[' ']
False
answered Apr 15, 2013 at 10:18
Raghav Aggarwal
Double each letter of a word in Python
In this Answer, we’ll learn how to use Python to take a word as input and output it with doubled letters.
Example
Input: red
Output:rreedd
Solution
To solve this problem, we’ll use the repetition and concatenation properties of strings.
String repetition
In Python, any string when multiplied with a numeric value results in a repetition of the original string.
For
example, when a string hello
is multiplied by a number 2
, the result is hellohello
.
string = "hello"
print[string*2]
String Repetition
Explanation
- Line 1: We store the string
hello
in the variablestring
. - Line 2: We print the result of the multiplication of a string with an integer.
String concatenation
When a string is added with another string, it results in the concatenation of both the strings.
For example, when a string hello
is added to the string world
, it results in the output helloworld
.
string1="hello"
string2="world"
print[string1+string2]
String Concatenation
Explanation
- Line 1: We store the string
hello
in the variablestring1
. - Line 2: We
store the string
world
in the variablestring2
. - Line 2: We print the result of the addition of a string with another string.
We need to repeat individual characters in the given problem instead of the whole word. To solve this, we will use both string repetition and concatenation together.
Solution
Take the input string and store it in a variable. Loop through the string using a for
loop, multiply every character by 2
,
and add the resulting character to an output
variable holding an empty string initially. This will double every character and add all the resulting characters to form an expected output string.
input_string = input[]
output=""
for i in input_string:
output = output + i*2
print[output]
Solution
Explanation
- Line 1: We store the input string in the variable
input_string
. - Line 2: We store an empty string
""
in the variableoutput
. - Line 3: We iterate through the string using a
for
loop. - Line 4: We multiply each character stored in the variable
i
with2
and add it to the variableoutput
. Store the result in the variableoutput
. - Line 5: We print the
output
variable.
CONTRIBUTOR
Raghav Aggarwal