コンテンツへスキップ
メニュー
この質問にフラグが付けられました
7 返信
89706 ビュー

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
7376
2
5月 22
33916
0
3月 19
4042
0
1月 19
5093
4
2月 24
12304