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]
Related
Let's say I have a string like this:
a = a = "\t\t\t\t"
If I print out the count of "\t" in the string, this is the output:
print(a.count("\t")) == output = 4 \
If I wanted to replace "\t" at any given occurrence in that string, how would I do it?
ex:
a.replace("\t", "a") #replacing the (first occurrence?) of "\t?
print(a.count("\t")) == output = 3
However, the "\t" is not getting replaced by "a" and therefore the "\t" count is still 4. Is there a way I can do this; preferably replace any given occurrence of "\t" within the string?
You can use regular expressions. It is another way to replace all the occurencies by specific pattern
try to use expandtabs, such as text = text.expandtabs(tabsize=4)
As Michael Butscher says, str.replace() returns a new string with replacements done. By default, all occurrences are replaced. Tabs are not a special case. This should do it:
a = a.replace("\t", "a")
print(a.count("\t"))
See: https://docs.python.org/3/library/stdtypes.html?highlight=str%20replace#str.replace
Thank you for the responses as they really opened the door to the solution. What I have done is converted a into a list:
a = "\t\t\t\t"
b = a.list()
#print(b):
#['\t', '\t', '\t', '\t']
I can then just use indices to replace what I need b[0] = "a", and then just convert it back into a str when I am done. This suits my purpose, but there are multiple ways of dealing with it like if I wanted to change all the "\t" into a "a" I could do a simple iteration like this:
for x in range(0, len(b)):
if b[x] == "\t":
b[x] = "a"
Of course the replace() function could do the same with it in string format, but at least with this iteration I could add more conditional logic if needed.
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
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 capture the sentence after a specific word. Each sentences are different in my code and those sentence doesn't necessarily have to have this specific word to split by. If the word doesn't appear, I just need like blank string or list.
Example 1: working
my_string="Python is a amazing programming language"
print(my_string.split("amazing",1)[1])
programming language
Example 2:
my_string="Java is also a programming language."
print(my_string.split("amazing",1)[1]) # amazing word doesn't appear in the sentence.
Error: IndexError: list index out of range
Output needed :empty string or list ..etc.
I tried something like this, but it still fails.
my_string.split("amazing",1)[1] if my_string.split("amazing",1)[1] == None else my_string.split("amazing",1)[1]
When you use the .split() argument you can specify what part of the list you want to use with either integers or slices. If you want to check a specific word in your string you can do is something like this:
my_str = "Python is cool"
my_str_list = my_str.split()
if 'cool' in my_str_list:
print(my_str)`
output:
"Python is cool"
Otherwise, you can run a for loop in a list of strings to check if it finds the word in multiple strings.
You have some options here. You can split and check the result:
tmp = my_string.split("amazing", 1)
result = tmp[1] if len(tmp) > 1 else ''
Or you can check for containment up front:
result = my_string.split("amazing", 1)[1] if 'amazing' in my_string else ''
The first option is more efficient if most of the sentences have matches, the second one if most don't.
Another option similar to the first is
result = my_string.split("amazing", 1)[-1]
if result == my_string:
result = ''
In all cases, consider doing something equivalent to
result = result.lstrip()
Instead of calling index 1, call index -1. This calls the last item in the list.
my_string="Java is also a programming language."
print(my_string.split("amazing",1)[1])
returns ' programming language.'
I am new to programming and am trying to work through computer science circles by the University of Waterloo and am stuck on this exercise: https://i.stack.imgur.com/ltVu9.png
The code in the image is what I have come up with so far.
The exercise wants me to take the substrings from before and after the "+" character and add them together using a for loop and I can't figure out how to get the substring. So far I've tried
print(S[0:len(char)])
To get the substring of the characters before and after the '+' symbol, you need to get the characters before the current position of the '+' char and after.
S = '12+5'
for pos in range(len(S)):
char = S[pos]
if char == '+':
sub_1 = S[:pos] # Get string before pos
sub_2 = S[pos + 1:] # Get string after pos
print('{} + {}'.format(sub_1, sub_2))
# Output: 12 + 5
However, if you are just wanting the simplest solution without thinking of how to do it manually then as others have said using .split() makes things easy. Since .split() will split a string into a list of strings separated by a specific character.
Using .split() the code can become like this:
S = '12+5'
split_S = S.split('+') # Creates a list of ['12', '5']
# Make sure our list has 2 items in it to print
if len(split_S) == 2:
print('{} + {}'.format(split_S[0], split_s[1])
# Output: 12 + 5
I would recommend this:
nums = S.split('+')
print(int(nums[0])+int(nums[1]))
sum([int(c) for c in input().split('+')])
5+12
Out: 17
Epicdaface25 is right, you could simply use
nums = S.split('+')
print(int(nums[0])+int(nums[1]))
But if you need to use a for loop, Karl's answer is the better option. However, you would not
print('{} + {}'.format(sub_1, sub_2))
you would need to print int(sub_1) + int(sub_2) to have Python actually add the two numbers and display the sum, as opposed to the mathematical expression.