0

I'm looking for a way to get the first letter of of a string. I already know about the .startswith() but that is for "if" statements I want something like

print text.startswith() # to give me the left most symbol
7
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by Thropian
1

You can also do string slicing:

s = 'hello'
print(s[:1])  # h

# more
print(s[:2])  # he
print(s[:4])  # hell

Edited by bumsfeld: more

0

While we're obfuscating

import re
string = "hello world"
first = re.match("(.)", string).group(1)
print(first)

Edited by Enders_Game: n/a

0

i agree with the splicing which i've found helpful.

"splicing" is that a secret python slicing method,just kidding :icon_wink:
"splicing" in action.

>>> s = 'hello'
>>> s[::-1][-1]
'h'
>>>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.