Skip to content

Instantly share code, notes, and snippets.

@RabeyaMuna
Last active October 6, 2022 08:35
Show Gist options
  • Save RabeyaMuna/8778b0a731ec6894cb6432dc6a14603a to your computer and use it in GitHub Desktop.
Save RabeyaMuna/8778b0a731ec6894cb6432dc6a14603a to your computer and use it in GitHub Desktop.
Want to try some string methods yourself? Give it a go! Fill in the gaps in the initials function so that it returns the initials of the words contained in the phrase received, in upper case. For example: "Universal Serial Bus" should return "USB"; "local area network" should return "LAN”.
def initials(phrase):
words = phrase.split()
result = ""
for word in words:
x=list(word)
if x[0].isupper():
result +=x[0]
elif x[0].islower():
result +=x[0].upper()
return result
print(initials("Universal Serial Bus")) # Should be: USB
print(initials("local area network")) # Should be: LAN
print(initials("Operating system")) # Should be: OS
@Laiba-Akram
Copy link

Laiba-Akram commented Oct 6, 2022

` def initials(phrase):
words =phrase.split()
result=""+""
for word in words:
result += word[0].upper()
return result

print(initials("Universal Serial Bus")) # Should be: USB
print(initials("local area network")) # Should be: LAN
print(initials("Operating system")) # Should be: OS

Here is output:
USB
LAN
OS

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment