콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
7 답글
89702 화면

Hi Friends,

I have to split a character from string.

For Example,If Name is like Sai Sathi Bawan,I have to split a first character from these strings as SSB and have to join these characters.How can we achieve this?Please help me in this friends.Thanks in advance

 

 

아바타
취소
베스트 답변

In python you can limit your string by using the VARIABLE[:NUMBER] format.

First character of a string

my_string[:1]

First five characters of a string

my_string[:5]

Last seven characters of a string

my_string[:-7]

So what you need is highlighted in bold

my_name = "Sai sathi bawan"
my_initals = ''.join([s[:1] for s in my_name.split(' ')])
print(my_initals)

Output: Ssb

To force Upper Case...

my_name = "Sai sathi bawan"
my_initals = ''.join([s[:1].upper() for s in my_name.split(' ')])
print(my_initals)

Output: SSB

아바타
취소

updated response

작성자

Thank you so much Stephen Mack for your response.Its mean a lot to me

Sure no problem. Have a good day.

Usefull Example Stephen Mack...

작성자

One more help.Mack i want to swap characters 6361 result as 6163 How it is possible??

베스트 답변

more on...python substring

http://net-informations.com/python/basics/substring.htm


아바타
취소
관련 게시물 답글 화면 활동
1
11월 15
7371
2
5월 22
33909
0
3월 19
4042
0
1월 19
5092
4
2월 24
12299