Here is a long string that I convert to a list so I can manipulate it, and then join it back together. I am having some trouble being able to have an iterator go through the list and when the iterator reach, let us say every 5th object, it should insert a '\n' right there. Here is an example:
string = "Hello my name is Josh I like pizza and python I need this string to be really really long"
string = string.split()
# do the magic here
string = ' '.join(string)
print(string)
Output:
Hello my name is Josh
I like pizza and python
I need this string to
be really really long
Any idea how i can achieve this?
I tried using:
for words in string:
if words % 5 == 0:
string.append('\n')
but it doesn't work. What am I missing?
What you're doing wrong is attempting to change string in your example which doesn't affect the string contained in your list... instead you need to index into the list and directly change the element.
text = "Hello my name is Josh I like pizza and python I need this string to be really really long"
words = text.split()
for wordno in range(len(words)):
if wordno and wordno % 5 == 0:
words[wordno] += '\n'
print ' '.join(words)
You don't want to call something string as it's a builtin module that is sometimes used and may confuse things, and I've also checked that wordno isn't 0 else you'll end up with a single word line in your rejoin...
The problem with the for loop you attempted to use, is that it didn't keep the index of the word, and thus could not determine which word that was the 5th. By using enumerate(iterable) you can get the index of the word, and the word at the same time. You could also just use range(len(iterable)) to get the index and just do it the same way.
string = "Hello my name is Josh I like pizza and python I need this string to be really really long"
string = string.split()
for word_num, word in enumerate(string):
if word_num and word_num % 5 == 0:
# access the array since changing only the variable word wont work
string[word_num] += '\n'
string = ' '.join(string)
print(string)
Edit:
As #JonClements pointed out, this causes "Hello" to be printed on its own line, because 0%5 = 0. Therefore I added a check to see if word_num evaluates to True (which it does if it is not equal to 0)
In this case you should probably be creating a new string instead of trying to modify the existing one. You can just use a counter to determine which word you're on. Here's a very simple solution, though Jon Clements has a more sophisticated (and probably more efficient) one:
newstring = ""
str = "Hello my name is Josh I like pizza and python I need this string to be really really long"
strlist = str.split()
for i, words in enumerate(strlist):
newstring += words
if (i + 1) % 5 == 0:
newstring += "\n"
else:
newstring += " "
`enumerate1 returns both the index of the word in your list of words, as well as the word itself. It's a handy automated counter to determine which word you're on.
Also, don't actually name your string string. That's the name of a module that you don't want to overwrite.
I am sure this could be much shorter but this does what you want. They key improvements are a new string to hold everything and the use of enumerate to catch the ith word in the list.
string = "Hello my name is Josh I like pizza and python I need this string to be really really long"
string2 = ""
for (i,word) in enumerate(string.split()):
string2 = string2 + " " + word
if (i + 1) % 5 == 0:
string2 = string2 + '\n'
print(string2)
you can use enumerate() on your string after you split it.
and iterate like that:
new_string_list = []
for index, word in string:
if (index + 1) % 5 == 0:
word += '\n'
new_string_list.append(word)
string = ' '.join(new_string_list)
Use enumerate to get index and % operation on i to append '\n' every n blocks
new_string_list = []
strlist = oldstr.split()
// do magic here
for i,word in enumerate(strlist):
new_string_list.append(word) #it is better
#to append to list then join, using + to concatenate strings is ineffecient
if count % 5 == 0:
new_string_list.append('\n')
print("".join(new_string_list))
Using list slicing...
s='hello my name is josh i like pizza and python i need this string to be really really long'
l=s.split()
l[4::5]=[v+'\n' for v in l[4::5] ]
print '\n'.join(' '.join(l).split('\n '))
hello my name is josh
i like pizza and python
i need this string to
be really really long
Horrible one-liner I cooked up as an example of what you should never do.
s='hello my name is josh i like pizza and python i need this string to be really really long'
print '\n'.join([' '.join(l) for l in [s.split()[i:i + 4] for i in range(0, len(s.split()), 4)]])
Related
I want to loop through a string and when it finds an uppercase letter, I want to replace it with #. Like this:
string = "hOw Are yOu?"
for x in string:
if x.isupper():
string.replace(x, "#")
print(string)
else:
print(string)
However, its not working as intended and is instead outputting the same string. Do tell me if there is a way to fix this or if you'd suggest another way.
Use list comprehension with join:
In [4]: ''.join([i if not i.isupper() else '#' for i in string])
Out[4]: 'h#w #re y#u?'
You just want to put the result again in string see below
string = "hOw Are yOu?"
for x in string:
if x.isupper():
string = string.replace(x, "#")
print(string)
else:
print(string)
Strings are immutable in Python. string.replace(x, "#") must thus be string = string.replace(x, "#") to have an effect on the string.
Note that currently your code has quadratic complexity as each replace operation has to loop over the entire string in linear time. A more efficient approach would be to perform the replacements yourself, as you're already looping over every character:
string = "".join(["#" if c.isupper() else c for c in "hOw Are yOu?"])
it would be even more concise (and possibly faster) to use a very simple RegEx for this:
import re
string = re.sub("[A-Z]", "#", "hOw Are yOu?")
this will fail for non-ASCII alphabets however; you'd have to use unicode properties & regex there.
This should do the trick!
string = "hOw Are yOu?"
for x in string:
if x.isupper():
string = string.replace(x, "#")
else:
pass
print(string)
I'm a novice as well! But from what I learned: when doing loops or if statements, you want to specify the value you are changing as I did in line 4 with: string = string.replace(x,'#') If not the change will not take effect!
Example:
my_list = [1,2,3,4]
for x in my_list:
my_list[x-1] = x + 1
print(my_list)
This is a poor example coding wise but it exemplifies the concept. If you don't address the variable it wont have any effect on it!
Hope this helps!
I'm trying to replace a given word in the string with the same number of '*'s and then print that new string. But it's not working
def censor(text , word):
l = len(word)
k = " " + str(word) + " "
if k in text:
text.replace(k , l*'*')
return text
print censor('this word duck and this word duck is censored', 'duck')
Strings are immutable. That means their methods don't change the object. If anything they return a new object. This is the case with str.replace. text.replace returns a modified string but you don't keep it, and text itself is not modified. You need to do this:
text = text.replace(k , l*'*')
I recommend reading the official Python tutorial.
i need to make a program that will capitalize the first word in a sentence and i want to be sure that all the special characters that are used to end a sentence can be used.
i can not import anything! this is for a class and i just want some examples to do this.
i have tried to use if to look in the list to see if it finds the matching character and do the correct split operatrion...
this is the function i have now... i know its not good at all as it just returns the original string...
def getSplit(userString):
userStringList = []
if "? " in userString:
userStringList=userString.split("? ")
elif "! " in userStringList:
userStringList = userString.split("! ")
elif ". " in userStringList:
userStringList = userString.split(". ")
else:
userStringList = userString
return userStringList
i want to be able to input something like this is a test. this is a test? this is definitely a test!
and get [this is a test.', 'this is a test?', 'this is definitely a test!']
and the this is going to send the list of sentences to another function to make the the first letter capitalized for each sentence.
this is an old homework assignment that i could only make it use one special character to separate the string into a list. buti want to user to be able to put in more then just one kind of sentence...
This may hep. use str.replace to replace special chars with space and the use str.split
Ex:
def getSplit(userString):
return userString.replace("!", " ").replace("?", " ").replace(".", " ").split()
print(map(lambda x:x.capitalize, getSplit("sdfsdf! sdfsdfdf? sdfsfdsf.sdfsdfsd!fdfgdfg?dsfdsfgf")))
Normally, you could use re.split(), but since you cannot import anything, the best option would be just to do a for loop. Here it is:
def getSplit(user_input):
n = len(user_input)
sentences =[]
previdx = 0
for i in range(n - 1):
if(user_input[i:i+2] in ['. ', '! ', '? ']):
sentences.append(user_input[previdx:i+2].capitalize())
previdx = i + 2
sentences.append(user_input[previdx:n].capitalize())
return "".join(sentences)
I would split the string at each white space. Then scan the list for words that contain the special character. If any is present, the next word is capitalised. Join the list back at the end. Of course, this assumes that there are no more than two consecutive spaces between words.
def capitalise(text):
words = text.split()
new_words = [words[0].capitalize()]
i = 1
while i < len(words) - 1:
new_words.append(words[i])
if "." in words[i] or "!" in words[i] or "?" in words[i]:
i += 1
new_words.append(words[i].capitalize())
i += 1
return " ".join(new_words)
If you can use the re module which is available by default in python, this is how you could do it:
import re
a = 'test this. and that, and maybe something else?even without space. or with multiple.\nor line breaks.'
print(re.sub(r'[.!?]\s*\w', lambda x: x.group(0).upper(), a))
Would lead to:
test this. And that, and maybe something else?Even without space. Or with multiple.\nOr line breaks.
I have a string eg:
line = "a sentence with a few words"
I want to convert the above in a string with each of the words in double quotes, eg:
'"a" "sentence" "with" "a" "few" "words"'
Any suggestions?
Split the line into words, wrap each word in quotes, then re-join:
' '.join('"{}"'.format(word) for word in line.split(' '))
Since you say:
I want to convert the above in a string with each of the words in double quotes
You can use the following regex:
>>> line="a sentence with a few words"
>>> import re
>>> re.sub(r'(\w+)',r'"\1"',line)
'"a" "sentence" "with" "a" "few" "words"'
This would take into consideration punctuations, etc, as well (if that is really what you wanted):
>>> line="a sentence with a few words. And, lots of punctuations!"
>>> re.sub(r'(\w+)',r'"\1"',line)
'"a" "sentence" "with" "a" "few" "words". "And", "lots" "of" "punctuations"!'
Or you can something simpler (more implementation but easier for beginners) by searching for each space in the quote then slice whatever between the spaces, add " before and after it then print it.
quote = "they stumble who run fast"
first_space = 0
last_space = quote.find(" ")
while last_space != -1:
print("\"" + quote[first_space:last_space] + "\"")
first_space = last_space + 1
last_space = quote.find(" ",last_space + 1)
Above code will output for you the following:
"they"
"stumble"
"who"
"run"
The first answer missed an instance of the original quote. The last string/word "fast" was not printed.
This solution will print the last string:
quote = "they stumble who run fast"
start = 0
location = quote.find(" ")
while location >=0:
index_word = quote[start:location]
print(index_word)
start = location + 1
location = quote.find(" ", location + 1)
#this runs outside the While Loop, will print the final word
index_word = quote[start:]
print(index_word)
This is the result:
they
stumble
who
run
fast
So I've been learning Python for some months now and was wondering how I would go about writing a function that will count the number of times a word occurs in a sentence. I would appreciate if someone could please give me a step-by-step method for doing this.
Quick answer:
def count_occurrences(word, sentence):
return sentence.lower().split().count(word)
'some string.split() will split the string on whitespace (spaces, tabs and linefeeds) into a list of word-ish things. Then ['some', 'string'].count(item) returns the number of times item occurs in the list.
That doesn't handle removing punctuation. You could do that using string.maketrans and str.translate.
# Make collection of chars to keep (don't translate them)
import string
keep = string.lowercase + string.digits + string.whitespace
table = string.maketrans(keep, keep)
delete = ''.join(set(string.printable) - set(keep))
def count_occurrences(word, sentence):
return sentence.lower().translate(table, delete).split().count(word)
The key here is that we've constructed the string delete so that it contains all the ascii characters except letters, numbers and spaces. Then str.translate in this case takes a translation table that doesn't change the string, but also a string of chars to strip out.
wilberforce has the quick, correct answer, and I'll give the long winded 'how to get to that conclusion' answer.
First, here are some tools to get you started, and some questions you need to ask yourself.
You need to read the section on Sequence Types, in the python docs, because it is your best friend for solving this problem. Seriously, read it. Once you have read that, you should have some ideas. For example you can take a long string and break it up using the split() function. To be explicit:
mystring = "This sentence is a simple sentence."
result = mystring.split()
print result
print "The total number of words is: " + str(len(result))
print "The word 'sentence' occurs: " + str(result.count("sentence"))
Takes the input string and splits it on any whitespace, and will give you:
["This", "sentence", "is", "a", "simple", "sentence."]
The total number of words is 6
The word 'sentence' occurs: 1
Now note here that you do have the period still at the end of the second 'sentence'. This is a problem because 'sentence' is not the same as 'sentence.'. If you are going to go over your list and count words, you need to make sure that the strings are identical. You may need to find and remove some punctuation.
A naieve approach to this might be:
no_period_string = mystring.replace(".", " ")
print no_period_string
To get me a period-less sentence:
"This sentence is a simple sentence"
You also need to decide if your input going to be just a single sentence, or maybe a paragraph of text. If you have many sentences in your input, you might want to find a way to break them up into individual sentences, and find the periods (or question marks, or exclamation marks, or other punctuation that ends a sentence). Once you find out where in the string the 'sentence terminator' is you could maybe split up the string at that point, or something like that.
You should give this a try yourself - hopefully I've peppered in enough hints to get you to look at some specific functions in the documentation.
Simplest way:
def count_occurrences(word, sentence):
return sentence.count(word)
text=input("Enter your sentence:")
print("'the' appears", text.count("the"),"times")
simplest way to do it
Problem with using count() method is that it not always gives the correct number of occurrence when there is overlapping, for example
print('banana'.count('ana'))
output
1
but 'ana' occurs twice in 'banana'
To solve this issue, i used
def total_occurrence(string,word):
count = 0
tempsting = string
while(word in tempsting):
count +=1
tempsting = tempsting[tempsting.index(word)+1:]
return count
You can do it like this:
def countWord(word):
numWord = 0
for i in range(1, len(word)-1):
if word[i-1:i+3] == 'word':
numWord += 1
print 'Number of times "word" occurs is:', numWord
then calling the string:
countWord('wordetcetcetcetcetcetcetcword')
will return: Number of times "word" occurs is: 2
def check_Search_WordCount(mySearchStr, mySentence):
len_mySentence = len(mySentence)
len_Sentence_without_Find_Word = len(mySentence.replace(mySearchStr,""))
len_Remaining_Sentence = len_mySentence - len_Sentence_without_Find_Word
count = len_Remaining_Sentence/len(mySearchStr)
return (int(count))
I assume that you just know about python string and for loop.
def count_occurences(s,word):
count = 0
for i in range(len(s)):
if s[i:i+len(word)] == word:
count += 1
return count
mystring = "This sentence is a simple sentence."
myword = "sentence"
print(count_occurences(mystring,myword))
explanation:
s[i:i+len(word)]: slicing the string s to extract a word having the same length with the word (argument)
count += 1 : increase the counter whenever matched.