字符串的格式化循环

时间:2016-10-10 22:15:52

标签: python function python-3.x

我在格式化下面的字符串时遇到了问题。试图使消息以边框为中心,但很难执行它。下面是我想要的输出,下面是我到目前为止的代码。注意:我需要在最长行的左右列上留一个空格,我需要使用split()函数。

+---------------+
|     STOP!     |
| DANGER AHEAD  |
| Drive Safely! |
+---------------+

def border_sign(note):
    letter_count = 0
    for i in note:
        letter_count += 1
        return "+-------+\n| {} |\n+-------+\n".format(note)
 border_sign("STOP\nDANGER AHEAD\nDrive safely!")

3 个答案:

答案 0 :(得分:2)

将字符串拆分为单独的行,然后将每行居中。记住要么在函数内立即打印每一行,建立一个列表,要么产生每一行而不是在第一行之后返回。您还可以确定并使用计算的宽度而不是静态值。

def border_sign(note):
    note = note.splitlines()
    m = len(max(note, key=len))
    yield ('-'*m).join('++')
    for line in note:
        yield ("|{:^%d}|" % m).format(line)
    yield ('-'*m).join('++')

print(*border_sign("STOP\nDANGER AHEAD\nDrive safely!"), sep='\n')

答案 1 :(得分:1)

要考虑的快速示例,而不是生产就绪代码:

def border_sign(note):
    rows = note.splitlines()
    mlen = len(max(rows, key=len))
    print "+" + "-" * (2+mlen) + "+"
    for row in rows:
        print ("| {:^"+str(mlen)+"} |").format(row)
    print "+" + "-" * (2+mlen) + "+"

答案 2 :(得分:0)

Import('common_env')
#Grab a copy of the top environment (the one sent by the SConstruct file)
common_env = common_env.Clone()
#Because this component is only compiled in win32.
if (common_env['ENV']['CONFIG'] == "win32"):
    #Grabs the library name, the name should look like libpath_of_current_component-(debug/opt)
    libName = common_env.libName()
    progName = 'testWinRTP.exe'
    common_env.USE_BOOST()
    common_env.USE_ACE()
    common_env.USE_LOKI()
    common_env.USE_DIRECTSOUND()
    #Specific cppflag for this component (appended to the flags sent from Sconstruct)
    cppFlags = '-D_AFXDLL'
    #Specific linkFlag for this component (appended to the flags sent from SConstruct)
    linkFlags = '/SUBSYSTEM:WINDOWS'
    #All the libraries the binary needs.
    libSuffix = common_env['ENV']['OPTSUFF'] + '.lib'

    common_env.Append(CPPFLAGS = cppFlags, LINKFLAGS = linkFlags)

    common_env.Append(LIBS=File('#/build/'+ common_env['ENV']['OPTSUFF'] +'/fwk/audio_fwk/src/tests/winRTP/testwinRTP/' + common_env['ENV']['CONFIG'] + '/libfwk_audio_fwk_src_tests_winRTP_testwinRTP-' + libSuffix))
    common_env.Append(LIBS=File('#/build/'+ common_env['ENV']['OPTSUFF'] +'/fwk/audio_fwk/src/tests/winRTP/CCNSMT/' + common_env['ENV']['CONFIG'] + '/libfwk_audio_fwk_src_tests_winRTP_CCNSMT-' + libSuffix))
    common_env.Append(LIBS=File('#/build/'+ common_env['ENV']['OPTSUFF'] +'/fwk/audio_fwk/src/filter_graph_mgt/'    + common_env['ENV']['CONFIG'] + '/libfwk_audio_fwk_src_filter_graph_mgt-' + libSuffix))
    common_env.Append(LIBS=File('#/build/'+ common_env['ENV']['OPTSUFF'] +'/fwk/audio_fwk/src/filter_graph/'        + common_env['ENV']['CONFIG'] + '/libfwk_audio_fwk_src_filter_graph-' + libSuffix))
    common_env.Append(LIBS=File('#/build/'+ common_env['ENV']['OPTSUFF'] +'/fwk/audio_fwk/src/filter_graph_drivers/'+ common_env['ENV']['CONFIG'] + '/libfwk_audio_fwk_src_filter_graph_drivers-' + libSuffix))
    common_env.Append(LIBS=File('#/build/'+ common_env['ENV']['OPTSUFF'] +'/fwk/audio_fwk/src/filter_graph_utils/'  + common_env['ENV']['CONFIG'] + '/libfwk_audio_fwk_src_filter_graph_utils-' + libSuffix))
    common_env.Append(LIBS=File('#/build/'+ common_env['ENV']['OPTSUFF'] +'/fwk/voice_fwk/utils/src/voice_utils/'   + common_env['ENV']['CONFIG'] + '/libfwk_voice_fwk_utils_src_voice_utils-' + libSuffix))
    common_env.Append(LIBS=File('#/build/'+ common_env['ENV']['OPTSUFF'] +'/fwk/voice_fwk/utils/src/config/'        + common_env['ENV']['CONFIG'] + '/libfwk_voice_fwk_utils_src_config-' + libSuffix))
    common_env.Append(LIBS=File('#/build/'+ common_env['ENV']['OPTSUFF'] +'/fwk/voice_fwk/utils/src/log_utils/'     + common_env['ENV']['CONFIG'] + '/libfwk_voice_fwk_utils_src_log_utils-' + libSuffix))
    common_env.Append(LIBS=File('#/build/'+ common_env['ENV']['OPTSUFF'] +'/fwk/voice_fwk/utils/src/vcs_utils/'     + common_env['ENV']['CONFIG'] + '/libfwk_voice_fwk_utils_src_vcs_utils-' + libSuffix))
    common_env.Append(LIBS='msacm32.lib')
    #Sources of the library.
    sourcesLib = ['CFilterGraphTest.cpp', 'stdafx.cpp', 'testWinRTPDlg.cpp']
    #Creates the library
    common_env.Library(libName, sourcesLib)
    #Compiles a ressource file needed for the binary
    compileRes = common_env.RES('testWinRTP.rc')
    #Creates the program, notice that the sources of this program includes the .res generated by the compilation of the .rc file.
    sourcesBin = ['testWinRTP.cpp', 'testWinRTP.res']
    common_env.Program(progName, sourcesBin)
    #Install (copy) the binary in LINK/bin/winX
    common_env.installInLink(progName)

def border_sign(x): splitted = x.splitlines() M = max(map(len,splitted)) horiz = '+-%s-+' % (M*'-') patiz = '| {0: ^%d} |' % M print(horiz,*map(patiz.format,splitted), horiz, sep='\n') border_sign("STOP\nDANGER AHEAD\nDrive safely!") 是一个函数,然后可以在patiz.format

中使用
相关问题