Assuming that the list is collection of strings, the first character of each string is obtained as follows − Show
>>> L1=['aaa','bbb','ccc'] >>> for string in L1: print (string[0]) a b c If 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 7
Updated on 18-Jun-2020 13:35:47
Lets say there is a string such as "this is a sample python program". How to write a program such that it would print the first character of each word in the string. such as T,I,A,S,P,P i.e. This Is A Sample Python Program ? Jun 1, 2018 in Python by• 170 points • 9,690 views 2 answers to this question.You can use split() using space as delimiter to fetch each word and then use counter-chindex to find if it is first character of each word. If yes, then apply upper() Below is the sample code: s1="this is a sample python program" • 200 points class Solution: def firstAlphabet(self, s): self.s=s k='' k=k+s[0] for i in range(len(s)): if s[i].isalpha(): pass else: k=k+s[i+1] return k answered Oct 28, 2020 by AnuragRelated Questions In Python
Subscribe to our Newsletter, and get personalized recommendations.Already have an account? Sign in. How do you find the first letter of each word in a string in python?Get the first character of a string in python
As indexing of characters in a string starts from 0, So to get the first character of a string pass the index position 0 in the [] operator i.e. It returned a copy of the first character in the string. You can use it to check its content or print it etc.
How do you get the first letter of each word in a string?To get the first letter of each word in a string: Call the split() method on the string to get an array containing the words in the string. Call the map() method to iterate over the array and return the first letter of each word. Join the array of first letters into a string, using the join() method.
How do you print the first occurrence of a character in a string in python?Use the find() Function to Find First Occurrence in Python
We can use the find() function in Python to find the first occurrence of a substring inside a string. The find() function takes the substring as an input parameter and returns the first starting index of the substring inside the main string.
How do I print the first letter of a name in Python?“how to print only the first letter in python” Code Answer's. # Get First 3 character of a string in python.. first_chars = sample_str[0:3]. print('First 3 characters: ', first_chars). # Output:. First 3 characters: Hel.. |