So I want input sentences like this:
i ) "abcdef"
relation
ii ) "xyzaswdawd"
relation
upto a few thousand rows.
I want to extract the sentences (i.e the text between " " for each line). Can anyone help me with that?
(Sorry if it is a naive question I am new to Python so wanted help with this)
Use regex for this:
import re
s = input()
print(re.findall(r'\"(.*)\"',s))
Would return a list of those words
mySTR = str(input("Input: "))
c=0
Res=""
for i in mySTR:
if i=='"':
if c!=0:
print(Res)
break
c=c+1
else:
Res=Res+i
The following code takes a string input. It starts with a count c and we assign it a value 0.
The count looks for the number for " found in your string.
It finds the first one and increments it by one. The next time it finds a " it will enter an if statement and print the Result.
The result will be anything that isn't a " and contained between two ". We concatenate every character
Related
I want to find a word in a string and then truncate the python string around that word.
Example:
str1 = "I want to try and select some specific thing in this world. Can you please help me do that"
Now I want to find the word specific in the string and then truncate the string from front and end to say 15 chars around that word.
So the answer would be something like:
"nd select some specific thing in this "
This is basically 15 characters left and right from "specific".
Thanks in advance
How about using the find() function, which returns the index of the first letter of the word to be searched, otherwise raises an exception:
x = "I want to try and select some specific thing in this world. Can you please help me do that"
word = "specific"
limit = 15
try:
index = x.find(word)
output = x[max(0, index-limit):min(len(x), index+limit+len(word))]
print(output)
except:
print("Word not found")
Oh and the x[:] is a method to splice the string, which is the python way to call a substring
The max and min functions prevent the substring's limits from going beyond the length of the original input
I have a long text that contains multiple paragraphs. It is stored in one variable.
I need to uppercase the first letter every word that follows a line break (or a period).
What is the most simple way to do that?
Here is a code that capitalize the the letter of every word of a new line.
texts=''' hiii.
hellooooooooo.
my name is stack.
dhdjljdkd.
'''
res=[ i.strip().capitalize() for i in texts.split('\n')]
print('\n'.join(res))
#for i in texts.split('\n'):
# res+=i.strip().capitalize()+'\n'
#print(res) #--- WORKS
Output:
Hiii.
Hellooooooooo.
My name is stack.
Dhdjljdkd.
I suppose you could split your text using as separator character \n. So the code should look like this:
output = []
for x in longString.split("\n"):
try:
x = x[0].upper() + x[1:]
except:
pass
output.append(x)
outputString = "\n".join(output)
With this approach you will be able to upper case the first letter after a line break. You can follow a similar approach for period.
Let me know if this helps! :D
Try like this.
May be it is a little complex.
I seem to have rebuilt the upper() function
import re
content="i have a long text that contains multiple paragraphs.\nit is stored in one variable.\ni need to uppercase the first letter every word that follows a line break (or a period).\nwhat is the most simple way to do that?"
def your_function_name(content):
new_res=[]
res=re.split(r'[.?\n]',content)
while "" in res:
res.remove("")
for con in res:
if 61<=ord(con[0])<=96:
new_con=con
new_res.append(new_con)
elif 97<=ord(con[0])<=123:
new_con=chr(ord(con[0])-32)+con[1:]
new_res.append(new_con)
else:
new_con=con
new_res.append(new_con)
return new_res
print("Transformed\n-----------------")
new_res=your_function_name(content)
for i in new_res:
print(i)
results are as follows
Transformed
-----------------
I have a long text that contains multiple paragraphs
It is stored in one variable
I need to uppercase the first letter every word that follows a line break (or a period)
What is the most simple way to do that
I am trying to solve a problem but struggling with the logic approach to it. The problem is as follows:
"Your task is to sort a given string. Each word in the string will contain a single number. This number is the position the word should have in the result.
Note: Numbers can be from 1 to 9. So 1 will be the first word (not 0).
If the input string is empty, return an empty string. The words in the input String will only contain valid consecutive numbers."
For e.g. "is2 Thi1s T4est 3a" --> "Thi1s is2 3a T4est"
I have tried the following so far:
def order(sentence):
my_string = "is2 Thi1s T4est 3a"
new_string = " "
for i in my_string:
if i in my_string == none
return new_string = " "
else:
if i in my_string
return new_string
But stuck on continuing the next bit. How can I put "put words into order starting from 1" into python code into my for loop?
I'm a beginner in python and programming so I am not entirely sure if the approach I'm making is the best logical way to do so, by creating an empty string new_string and then sorting my_string into that. Is this a good way of approaching this? I am stuck on which direction to go after this.
you could do :
r = "is2 Thi1s T4est 3a"
def get_number(w) :
for x in w :
try :
i = int(x)
return i
except :
pass
return None
ordered_list = " ".join((sorted( s.split(' ') , key = get_number)))
the function get_number allows to get the first number appearing in a word, so we split the sentence to get the words it's made of , r.split(' ') gives ['is2', 'Thi1s', 'T4est', '3a'], we order then the list using ordered builtin function , it takes a list and a function that yields the keys over which we want to order the list, and outputs the ordered list, we then use the builtin function join to join the list using a space separator.
Output :
Out[425]: 'Thi1s is2 3a T4est'
I have been trying to make a Python code "program that reads in a word specified by the user and prints that word out x number of times where x is the number of characters in the word. Your solution must use a loop"
For example, if the user enters “John”, the computer will print
“John” four times since there are four characters in “John”.
I have done my other task for today though it took me a while it was to make a code that does the same but for the amount of words in a paragraph then prints them as many times as there were words in the paragraph.
loop = 0
print ("please enter a paragraph")
word = str (input())
words = word.split()
number_of_words = len(words)
while (loop < number_of_words):
print(words)
loop = loop + 1
I feel i'm not far off the same principle for the task I am asking help for, as my other does the same but with words not letters. May someone show me the most basic way to count all the letters in a paragraph then print them as many times as there is letters in the paragraph . I found one post on here that is almost the same but refers to files and looks a tad too complex for me "being in my first year at college"
assuming that your variable name is your input:
for _ in name: print(name)
for loops iterate through the letters of a string but since you want to print out the word each time, you dont need to use the values.
python makes this extremely easy:
print(word*len(word))
# or with newlines inbetween
print((word+"\n")*len(word))
Edit: since you really need a loop just use the already suggested answer from R Nar:
for _ in word: print(word)
for i in range(0, len(words)):
print words
I think this should be fine.
hey i want to be able to change my string being split at say the third full stop or the 2nd here is my code
file = "hey there. this is some demo code. will you nice people please help me."
i want to split the string after the 2nd full stop so it will look like
"hey there. this is some demo code."
".".join(file.split(".")[2:])
or
file.split(".",2)[2:]
These use str.split() (2/3), str.join() (2/3), and slices (2/3). There's no reason to use loops or regex for this.
I would do something like this:
readf = open("split.txt")
for line in readf:
a=line.split(".")
readf.close()
print (a[:2])
Basically you store the line in a and split it on "." and then use a subsequence which you can use however you like.
e.g. a[2:3] gives u the secound and third line while a[:3] gives you all three.
A rough way to do it would be to use a counter and loop through the string, adding every character until the stopping limit is reached.
firstString = ""
secondString = ""
stopsAllowed = 1
stopsFound = 0
for i in range(0,len(file)):
if file[i] == ".":
stopsFound += 1
if stopsFound <= stopsAllowed:
firstString += file[i]
else:
secondString += file[i]