I am working on a simple program that finds all the capitalized letters in an inputed string. However, I am stuck on getting the program to function properly. I did the bulk of the work, but I do not see why it does not work.
import string
def string_upper(capz):
BigLetters = input
input = "Please enter a string"
for char in capz:
if char.isupper():
BigLetters += char
return BigLetters
print(string_upper)
Please keep in mind that I am relatively new to Python, with only roughly 3 months of experience. Any help is appericated!
Try this, it will give you a list of uppercase letters
def string_upper():
big_letters = input("Enter String: ")
uppers = [l for l in big_letters if l.isupper()]
return uppers
if __name__ == '__main__':
print(string_upper())
Or in one line of code
def find_caps():
return [l for l in input("Enter String: ") if l.isupper()]
if __name__ == '__main__':
print(find_caps())
if you don't want it as a list you can do this to get it back to a string
print("".join(find_caps()))
HexxNine already gave you a great answer, but I'll add some explanation as to what is wrong with what you posted.
def string_upper():
chars = input("Please enter a string: ")
cap_chars = []
for char in chars:
if char.isupper():
cap_chars += char
return cap_chars
print(string_upper())
You do not need to add import string in the beginning. If you are trying to get the initial string from the user, your function string_upper also does not need any arguments. In Python 3, input is a built-in function where the query prompt to the user ("Please enter a string") needs to be the input function's argument (comes in parentheses after input). I created an empty list cap_chars above which is then used to store any uppercase letters. You cannot call your function string_upper without at least empty parentheses behind it, ie you need to use string_upper() in case the function does not take any arguments or string_upper(some_argument) in case it takes a single argument.
If you do not want user input to be a direct part of the function, you can also pass a string as an argument or pass the user input like this:
def string_upper(chars):
cap_chars = []
for char in chars:
if char.isupper():
cap_chars += char
return cap_chars
print(string_upper("ThiS iS a sTrInG")) # just pass some string
print(string_upper(input("Please enter a string: "))) # pass user input
HexxNine used a list comprehension in his solution which is a more elegant approach and is deemed "pythonic" because, well, list comprehensions are one of the features which make Python so neat. You might want to look up what a list comprehension is. Getting rid of another line:
def string_upper():
chars = input("Please enter a string: ")
return [c for c in chars if c.isupper()]
print(string_upper())
This would also work but is not as readable anymore, so I'd recommend using the previous solution:
def string_upper():
return [c for c in input("Please enter a string: ") if c.isupper()]
print(string_upper())
Str="hello World this is newBie"
import string
ref=[i for i in string.ascii_uppercase]
capitals=[i for i in Str if i in ref]
print capitals
this could be more abstract solution
You can just do this:
import re
your_input_string = "Hi, this is a Test String"
capitals = re.findall(r'[A-Z]{1}', your_input_string) # ['H', 'T', 'S']
I am using Regular Experessions to match capital letters in your_input_string
.
[AZ]
will match any capital letter while {1}
says to match single letters. findall()
function simply returns a list of matched strings and hence we get a list of capital letters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.