How to Print Just the First Letters of Each Word

How to print just the first letters of each word?

Try using this code:

 title_split = title.split()     
new=[]
for title in title_split:
letter=title[0].upper()
new.append(letter)
ans=" ".join(new)
print(musical+",",ans)

Hope it works :)

Get the first letter of each word in a string

explode() on the spaces, then you use an appropriate substring method to access the first character of each word.

$words = explode(" ", "Community College District");
$acronym = "";

foreach ($words as $w) {
$acronym .= mb_substr($w, 0, 1);
}

If you have an expectation that multiple spaces may separate words, switch instead to preg_split()

$words = preg_split("/\s+/", "Community College District");

Or if characters other than whitespace delimit words (-,_) for example, use preg_split() as well:

// Delimit by multiple spaces, hyphen, underscore, comma
$words = preg_split("/[\s,_-]+/", "Community College District");

How can I get the first letter of each word in a string?

[ s[0] for s in 'Piethon is good'.split() ]

print out first letter of each item in a list

For input:

  • in a loop, ask the user a word, if it nothing just stop
  • if it's a word, save it in sentence and it's first letter in acrostic (word[0] not sentence[0])

For output:

  • for the sentence join the words with a space : " ".join(sentence)
  • for the acrostic, join the letters with nothing : "".join(acrostic)
sentence = []
acrostic = []
while True:
word = input('Please enter a word, or enter to stop : ')
if not word:
break
sentence.append(word)
acrostic.append(word[0].upper())

print(" ".join(sentence))
print("-- {}".format("".join(acrostic)))

Gives

Please enter a word, or " to stop : A
Please enter a word, or " to stop : cross
Please enter a word, or " to stop : tick
Please enter a word, or " to stop : is
Please enter a word, or " to stop : very
Please enter a word, or " to stop : evil
Please enter a word, or " to stop :
A cross tick is very evil
-- ACTIVE

How to print the first letter from all elements in a list?

There are perhaps more pythonic ways to do this, but for the smallest code change, all you'd need to do is to add to a string and print it after looping. So, instead of printing z[0] you would add it to a string which would need to be instantiated prior to the loop. Then you could add the last-name to it before finally printing the entire string. i.e.

name=input()
name=name.split(' ')
lname = name[-1]
fname= name[:-1]
result = ""
for z in fname:
result = result + z[0] + ". "
result = result + lname
print(result)

I would instead do the following:

name=input()
name=name.split(' ')
print('. '.join([n[0] if i != len(name) - 1 else n for i, n in enumerate(name)]))

which given Stack Over Flow returns S. O. Flow

This does not capitalize the letters for you, so if provided stack over flow it would return s. o. flow.

To break down the code I provided, the '. '.join(<list>) takes a list of strings and concatenates them together using the string '. '. This means that the code inside the join is providing the ['s', 'o', 'flow'].

The code inside the join is called a list comprehension. It's effectively a flattened for loop which appends each result of the for loop to a list. Section 5.1.3 of the python tutorial covers list comprehensions.

That list comprehension takes the first letter of the name unless the index (i) of that string is equal to the last index of the list. This means with the input Stack Over Flow the len() is 3. The last index is 2 (counting from 0 --> 0=='Stack', 1=='Over', 2=='Flow'). Once the index == 2, it takes the entire name instead of the first letter.

Enumerate gives you both its index in the list and the element of the list which is why my for loop is for i, n instead of just for n.

The advantage of not hard-coding a value such as saying == 2, is for other inputs. What if a person does not have a middle name? E.g. John Doe should be J. Doe. With a hard-coded value of 2, the result would be J. D. instead. Or what if the input was for Mary Sue Elizabeth Smith? Her result should be M. S. E. Smith, but would not result in that output if the number of names is hard-coded in your solution.



Related Topics



Leave a reply



Submit