Python - making a function that would add "-" between letters -


i'm trying make function, f(x), add "-" between each letter:

for example:

f("james") 

should output as:

j-a-m-e-s- 

i love if use simple python functions new programming. in advance. also, please use "for" function because i'm trying learn.

edit:

yes, want "-" after "s".

also, please use "for" function because i'm trying learn

>>> def f(s):         m = s[0]         in s[1:]:              m += '-' +         return m  >>> f("james") 'j-a-m-e-s' 
  • m = s[0] character @ index 0 assigned variable m

  • for in s[1:]: iterate second character ,

  • m += '-' + i append - + char variable m

  • finally return value of variable m

if want - @ last this.

>>> def f(s):         m = ""         in s:             m +=  + '-'         return m  >>> f("james") 'j-a-m-e-s-' 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -