+ 15

Another Python question...

Hi guys, just wondering how to find the first letter of a word in Python? Is there a special keyword? I'm a beginner in Python, so sorry if you think this is a silly question 😐 Anyway, thanks 😊

21st Dec 2017, 9:55 PM
Learnsolo
6 Respuestas
+ 8
word = input("Enter the word: ")) print(word[0]) #The input is a string, so the first letter of the word is the 0th index. 😉
21st Dec 2017, 10:20 PM
blackcat1111
blackcat1111 - avatar
+ 17
Thank you @LunarCoffee! 😊 I am interested in learning Java, have started a bit already ☺
22nd Dec 2017, 3:02 AM
Learnsolo
+ 15
Thank you @blackcat1111 😊☺
21st Dec 2017, 11:31 PM
Learnsolo
+ 11
Python has a special indexing system for strings! [start:stopbefore:steps] for example, [1:7:2] takes characters from position one, then moves 2 chars forward then picks the next one, until it is <=7. All these parameters are optional. In this case, it is [0] the index you ned ti use The explanation on top was for any general case in string indexing you might have queries in. Feel free to ask me if you have any doubts 😉
23rd Dec 2017, 4:05 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
For future reference, a string is just an array of chars. Keep that in mind if you're going to learn and use other languages like Java. toCharArray() has uses.
22nd Dec 2017, 1:17 AM
LunarCoffee
LunarCoffee - avatar
+ 1
foo = input("Enter the word: ")) print(word[0])
23rd Dec 2017, 6:20 AM
Renice Owino
Renice Owino - avatar