如何在不使用任何功能的情况下在每个字母后添加星号Python

我如何在每个字母后加一个星 在Python中不使用任何功能

example : MOHAMED >>>>> M*O*H*A*M*E*D
评论
  • _堅=強
    _堅=強 回复
    source = 'MOHAMED'
    starred = ''
    for char in source:
        starred += char + '*'
    starred = starred[:-1] # cut last '*'
    
  • kqui
    kqui 回复

    Using str.join() - not sure what you mean by "without using any function" - I assume you mean without defining a function.

    >>> s = 'MOHAMED'
    >>> '*'.join(s)
    'M*O*H*A*M*E*D'
    

    或使用for循环:

    s = 'MOHAMED'
    rv = ''
    
    for c in s:
        rv += c + '*'
    
    rv = rv[:-1]
    

    输出:

    >>> rv
    'M*O*H*A*M*E*D'