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
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
Strings are indexable. text[0]
is the first symbol.
>>> d="hello"
>>> d[0]
'h'
>>>
from idle.
You can also do string slicing:
s = 'hello'
print(s[:1]) # h
# more
print(s[:2]) # he
print(s[:4]) # hell
i agree with the splicing which i've found helpful.
While we're obfuscating
import re
string = "hello world"
first = re.match("(.)", string).group(1)
print(first)
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'
>>>
cool thanks guys
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.