0

i just wanna to know how can i print the names with first capital letter in the list and ignore who starts with lowercase in python with while Loop

# Input
friends = ["Mohamed", "Shady", "ahmed", "eman", "Sherif"]

# Needed Output
"Mohamed"
"Shady"
"Sherif"
"Friends Printed And Ignored Names Count Is 2"

5 Answers5

2

You can use ord builtin function to determine if the character is uppercase or lowercase. You can also use the isupper method. Here is the sample code:

print([f for f in friends if f[0].isupper()])
Deniz Kaplan
  • 1,549
  • 1
  • 13
  • 18
0

If you want to only those values have the first uppercase letter.

friends = ["Mohamed", "Shady", "ahmed", "eman", "Sherif"]


for a in friends:
    if a[0].isupper():
        print(a)

Output

Mohamed
Shady
Sherif

You can try this too

friends = ["Mohamed", "Shady", "ahmed", "eman", "Sherif"]

uppercase = list(filter(lambda e: e[0].istitle(),friends) )
lowercase = list(filter(lambda e: e[0].islower(),friends))


print(uppercase)
print(lowercase)

OUTPUT

['Mohamed', 'Shady', 'Sherif']
['ahmed', 'eman']

If you want only the titleCase "(use for a name.)" string You can use str.istitle().

friends = ["Mohamed", "Shady", "ahmed", "eman", "Sherif"]


for a in friends:
    if a.istitle():
        print(a)

OUtput

Mohamed
Shady
Sherif
codester_09
  • 5,622
  • 2
  • 5
  • 27
0

The isupper() function can be used for this.

Ref: https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/

friends = ["Mohamed", "Shady", "ahmed", "eman", "Sherif"]

# To print the words which start with Uppercase.
for val in friends:
    if val[0].isupper():
        print(val)
0

Maybe not the most elegant solution, but works as long as you are ignoring typos in names like "MOhamed":

# Input
friends = ["Mohamed", "Shady", "ahmed", "eman", "Sherif"]

uppercase = []
lowercase = []

for friend in friends:
    if friend[0].isupper() == True:
        uppercase.append(friend)
    else: 
        lowercase.append(friend)

for name in uppercase: 
    print(name)
    
print("Ingnored names: {}".format(len(lowercase)))
chris
  • 170
  • 1
  • 9
0

You can use for loop and compare the element of list to str.capitalize() with while then print the elements.

You also need ignored counter to count element that not capitalize to print in the last statement:

friends = ["Mohamed", "Shady", "ahmed", "eman", "Sherif", "BATMAN", 'deJavu']

ignored = 0

for friend in friends:
    while friend != friend.capitalize():
        ignored += 1
        break
    else:
        print(friend)

print(f"Friends Printed And Ignored Names Count Is {ignored}")

# Mohamed
# Shady
# Sherif
# Friends Printed And Ignored Names Count Is 4
Arifa Chan
  • 947
  • 2
  • 6
  • 23