Print the string, if string in the line ends with specific characters - python

How do I print every string that ends on 'je' in a line
For example :
for line in sys.stdin:
for string in line:
if string ends with 'je':
print string
And only if the string ends with '....je' , so no 'je' included. And if the string ends with '....je?' or '....je.' remove '? , . ' . Then it should print the string. Help would be appreciated.

Assuming that your string name dosent allow '?,.' at all, it should work.
for line in sys.stdin:
for string in line:
string = string.strip('.,?')
if string.endswith('je'):
print(string)

There are two problems in your code. for string in line: will go through every single character, not through every word. By the way, you should not name your variable string. for word in line.split(): will do what you expect.
Also, if string ends with 'je': should be if word.endswith('je'):. If you don't want to match the exact word "je", you can change that to if word.endswith('je') and word != "je":.
Final code, including the removal of ! and ?:
f = open("a.txt", "r")
for line in f:
for word in line.split():
word = word.rstrip("!?")
if word.endswith('je') and word != "je":
print(word)
Note that if a word ends with several ? or !, they will all get removed.

Related

How to capitalize the first and last letter of a string?

So it's not a very difficult problem and I've been trying to do it. Here is my sample code:
import sys
for s in sys.stdin:
s = s[0:1].upper() + s[1:len(s)-1] + s[len(s)-1:len(s)].upper()
print(s)
This code only capitalizes the first letter and not the last letter as well. Any tips?
You are operating on lines, not words, since iterating over sys.stdin will give you strings that consist of each line of text that you input. So your logic won't be capitalizing individual words.
There is nothing wrong with your logic for capitalizing the last character of a string. The reason that you are not seeming to capitalize the end of the line is that there's an EOL character at the end of the line. The capitalization of EOL is EOL, so nothing is changed.
If you call strip() on the input line before you process it, you'll see the last character capitalized:
import sys
for s in sys.stdin:
s = s.strip()
s = s[0:1].upper() + s[1:len(s)-1] + s[len(s)-1:len(s)].upper()
print(s)
#Calculuswhiz's answer shows you how to deal with capitalizing each word in your input.
You first have to split the line of stdin, then you can operate on each word using a map function. Without splitting, the stdin is only read line by line in the for loop.
#!/usr/bin/python
import sys
def capitalize(t):
# Don't want to double print single character
if len(t) is 1:
return t.upper()
else:
return t[0].upper() + t[1:-1] + t[-1].upper()
for s in sys.stdin:
splitLine = s.split()
l = map(capitalize, splitLine)
print(' '.join(l))
Try it online!
You could just use the capitalize method for str which will do exactly what you need, and then uppercase the last letter individually, something like:
my_string = my_string.capitalize()
my_string = my_string[:-1] + my_string[-1].upper()

Exclude list of letters in a string from list of words

I am a bit in trouble in a way to select a bunch of letters in a for loop that select words in a list which do not contain these letters;
Bellow the code I am trying to use, when I assign string with just one char, it print all words in words.txt that do not contain that char, but if I assign string with more than one char it consider the whole string, even using a ( for letter in string )
def avoids_2():
string = 'abc'
fin = open('words.txt')
for letter in string:
for line in fin:
if letter not in line:
word = line.strip()
print(word)
Please, does anyone knows how to solve ?
Issue with your code is that you are searching for only one character at a time i.e if your letter is c it will look for only c and print strings with a or b too in the internal loop. So you can change it like this
fin = open('words.txt')
test_string = 'abc'
for line in fin:
flag = False
for letter in test_string:
if letter in line:
flag = True
break
if not flag:
print(line.strip())
You can also use regex here
import re
fin = open('words.txt')
for line in fin:
if re.match(r'a|b|c', line):
continue
print(line.strip())

Python Code to Replace First Letter of String: String Index Error

Currently, I am working on parsing resumes to remove "-" only when it is used at the beginning of each line. I've tried identifying the first character of each string after the text has been split. Below is my code:
for line in text.split('\n'):
if line[0] == "-":
line[0] = line.replace('-', ' ')
line is a string. This is my way of thinking but every time I run this, I get the error IndexError: string index out of range. I'm unsure of why because since it is a string, the first element should be recognized. Thank you!
The issue you're getting is because some lines are empty.
Then your replacement is wrong:
first because it will assign the first "character" of the line but you cannot change a string because it's immutable
second because the replacement value is the whole string minus some dashes
third because line is lost at the next iteration. The original list of lines too, by the way.
If you want to remove the first character of a string, no need for replace, just slice the string (and don't risk to remove other similar characters).
A working solution would be to test with startswith and rebuild a new list of strings. Then join back
text = """hello
-yes--
who are you"""
new_text = []
for line in text.splitlines():
if line.startswith("-"):
line = line[1:]
new_text.append(line)
print("\n".join(new_text))
result:
hello
yes--
who are you
with more experience, you can pack this code into a list comprehension:
new_text = "\n".join([line[1:] if line.startswith("-") else line for line in text.splitlines()])
finally, regular expression module is also a nice alternative:
import re
print(re.sub("^-","",text,flags=re.MULTILINE))
this removes the dash on all lines starting with dash. Multiline flag tells regex engine to consider ^ as the start of the line, not the start of the buffer.
this could be due to empty lines. You could just check the length before taking the index.
new_text = []
text="-testing\nabc\n\n\nxyz"
for line in text.split("\n"):
if line and line[0] == '-':
line = line[1:]
new_text.append(line)
print("\n".join(new_text))

Checking characters in an input file line, and returning if they contain only valid characters

So I want to run a program that will read in a file line by line, and will then print out either Valid or Invalid based on what each line contains.
For this example, I am saying that the input file line can contain ABCabc or a space. If the line only contains these things, the word Valid should be printed. If it is just white space, or contains any other characters or letters, it should print out “Invalid”.
This is what I have come up with:
I can’t seem to get it to ever print out “Valid”
Can you tell why? Thanks!
input = sys.argv[1]
input = open(input,"r")
correctInput = ‘ABCabc ‘
line1 = input.readline()
while line1 != "":
if all(char in correctInput for char in line1):
print “Valid”
line2 = input.readline()
else:
print “Invalid”
line2 = input.readline()
line1 = line2
If you print out the value of line1 before your if else statement, you'll see it has a newline character in it. (The \n character.) This is the character that gets added to the end of each line whenever you hit the enter key on the keyboard, and you need to either discard the newline characters or include them as valid input.
To include it as valid input
Change correctInput = 'ABCabc '
to
correctInput = 'ABCabc \n'.
Or to discard the newline characters change
if all(char in correctInput for char in line1):
to
if all(char in correctInput for char in line1.replace('\n', '')):
Either method will work.
Bytheway, input is a function in Python. Although you're allowed to use it as a variable name, doing so will prevent you from using the input function in your program. Because of this, it is considered bad practice to use any of the built in function names as your variable names.
RegEx Solution
Just for fun, I came up with the following solution which solves your problem using regular expressions.
import re
with open(sys.argv[1]) as fh:
valid_lines = re.findall('^[ABCabc ]+\n', fh.read())
This finds any valid lines using the pattern '^[ABCabc ]+\n'. What does this regular expression pattern do?
The ^ symbol signifies the start of a line
Then comes the [ABCabc ]. When brackets are used, only characters inside of those brackets will be allowed.
The + after the brackets means that those characters that where in brackets must be found 1 or more times.
And lastly we make sure the valid characters we found are followed by a newline character (\n). This ensures we checked the complete line for valid characters.
Its because readline doesn't remove '\n' from end of the line. You could ignore that problem by splitting whole file content in lines and than validate them one by one.
import sys
file_name = sys.argv[1]
file = open(file_name ,"r")
correctInput = 'ABCabc '
lines = file.read().splitlines()
for line1 in lines:
if all(char in correctInput for char in line1):
print 'Valid'
else:
print 'Invalid'

How do I output the acronym on one line

I am following the hands-on python tutorials from Loyola university and for one exercise I am supposed to get a phrase from the user, capatalize the first letter of each word and print the acronym on one line.
I have figured out how to print the acronym but I can't figure out how to print all the letters on one line.
letters = []
line = input('?:')
letters.append(line)
for l in line.split():
print(l[0].upper())
Pass end='' to your print function to suppress the newline character, viz:
for l in line.split():
print(l[0].upper(), end='')
print()
Your question would be better if you shared the code you are using so far, I'm just guessing that you have saved the capital letters into a list.
You want the string method .join(), which takes a string separator before the . and then joins a list of items with that string separator between them. For an acronym you'd want empty quotes
e.g.
l = ['A','A','R','P']
acronym = ''.join(l)
print(acronym)
You could make a string variable at the beginning string = "".
Then instead of doing print(l[0].upper()) just append to the string string += #yourstuff
Lastly, print(string)

Categories

Resources