How to print out the first character of each list in Python?
PythonServer Side ProgrammingProgramming
Assuming that the list is collection of strings, the first character of each string is obtained as follows −
>>> L1=['aaa','bbb','ccc'] >>> for string in L1: print (string[0]) a b cIf list is a collection of list objects. First element of each list is obtained as follows −
>>> L1=[[1,2,3],[4,5,6],[7,8,9]] >>> for list in L1: print (list[0]) 1 4 7Jayashree
Published on 15-Feb-2018 08:16:18
Previous Page Print Page
Next Page
Advertisements
String containing first letter of every word in a given string with spaces
String str is given which contains lowercase English letters and spaces. It may contain multiple spaces. Get the first letter of every word and return the result as a string. The result should not contain any space.
Examples:
Input : str = "geeks for geeks" Output : gfg Input : str = "happy coding" Output : hcSource: //www.geeksforgeeks.org/amazon-interview-set-8-2/amp/
Example How tocapitalize the first letter of every word in the list
Python simple code.
list1 = ["Hello", "how", "are", "you"] for i in range(len(list1)): list1[i] = list1[i].capitalize() print(list1)Output:
Another example
You can also use the python string title method with the same process.
list1 = ["Hello", "how", "are", "you"] for i in range(len(list1)): list1[i] = list1[i].title() print(list1)Output: [‘Hello’, ‘How’, ‘Are’, ‘You’]
Do comment if you have any doubts and suggestions on this python list topic.
Note: IDE:PyCharm2021.3 (Community Edition)
Windows 10
Python 3.10.1
AllPython Examplesare inPython3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.
Share this:
- More
- Tumblr
- Telegram
- Skype