如何将字符串数组拆分为2个serperately部分

时间:2018-05-16 07:33:50

标签: python

如何在一些单独的部分中拆分字符串数组? 我可以单独打印

我甚至不知道如何开始 任何人都可以帮助我推动正确的方向

谢谢

2 个答案:

答案 0 :(得分:0)

Python仅将字符串存储为列表。让我们说你有:

s = 'abcd'

您可以使用substring

的概念单独访问列表
s[1:2]
>> b
s[:3]
>>abc

同样,For

s = ['hello my name is anton','I am 9999 years old']
''.join(s[1])
''.join(s[1:2])

您可以查看List Slicing

答案 1 :(得分:0)

<tfoot>
        <?php
            if ( $totals = $order->get_order_item_totals() ) {
                $i = 0;
                foreach ( $totals as $total ) {
                    $i++;
                    ?>
                   <?php if($total['label'] == "Shipping:" || $total['label'] == "Verzending:"){ ?>

                   <?php } elseif($total['label'] == "Subtotal:" || $total['label'] == "Subtotaal:") {
                   $oid = $order->get_id(); ?>
                   <tr>
                        <th class="td" scope="row" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo "Subtotaal"; ?></th>
                        <td class="td" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php $order_st=get_post_meta($oid, 'order_subtotal', true); echo "<span class='woocommerce-Price-currencySymbol'>".get_woocommerce_currency_symbol()."</span>".$order_st."245.00"; ?></td>
                    </tr>
                    <?php $trans_method=get_post_meta($oid, 'trans_method', true); if($trans_method == 'pickup'){ ?>
                    <tr>
                        <th class="td" scope="row" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo "Transportkosten"; ?></th>
                        <td class="td" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo "<span class='woocommerce-Price-currencySymbol'>".get_woocommerce_currency_symbol()."</span>0.00"; ?></td>
                    </tr>
                    <tr>
                        <td colspan="2" class="td" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo "Afhalen"; ?></td>
                    </tr>
                    <tr>
                        <th class="td" scope="row" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo "Totaal ex BTW"; ?></th>
                        <td class="td" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php $teb=get_post_meta($oid, 'order_subtotal', true); echo "<span class='woocommerce-Price-currencySymbol'>".get_woocommerce_currency_symbol()."</span>".$teb; ?></td>
                    </tr>
                    <?php } ?>
                    <?php if($trans_method == 'deliverybypostcode'){ ?>
                    <tr>
                        <th class="td" scope="row" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo "Transportkosten"; ?></th>
                        <td class="td" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><span class="woocommerce-Price-currencySymbol"><?php $transcost=get_post_meta($oid, 'tran_cost', true); echo get_woocommerce_currency_symbol();  ?></span><?php echo money_format("%.2n", $transcost); ?></td>
                    </tr>
                    <tr><td colspan="2" class="td" scope="row" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo "Bezorgen op postcode:"; ?> <?php $postcode=get_post_meta($oid, 'trans_location', true);
                    $place_dis=get_post_meta($oid, 'trans_distance', true);
                    $mcrane=get_post_meta($oid, 'mounted_crane', true);
                     echo $postcode[0]." ".$place_dis."</br>";
    if($mcrane == "Yes"){
        echo "Transport: "."Met autolaadkraan"."</td></tr>";
    }else{
        echo "Transport: "."Zonder autolaadkraan"."</td></tr>";
    } ?>
    <tr>
                        <th class="td" scope="row" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo "Totaal ex BTW"; ?></th>
                        <td class="td" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php $os_total=get_post_meta($oid, 'order_subtotal', true);
                         $tcost=get_post_meta($oid, 'tran_cost', true);
                        $ot_cost=$os_total+money_format("%.2n", $tcost);
                          echo "<span class='woocommerce-Price-currencySymbol'>".get_woocommerce_currency_symbol()."</span>". money_format("%.2n", $ot_cost); ?></td>
                    </tr>
    <?php } ?>
                   <?php } elseif($total['label'] == "BTW:"){ 
                    $oid1 = $order->get_id();
                    ?>
                   <tr>
                        <th class="td" scope="row" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo $total['label']; ?></th>
                        <td class="td" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php $ttax = get_post_meta($oid1, 'order_total_tax', true); echo "<span class='woocommerce-Price-currencySymbol'>".get_woocommerce_currency_symbol()."</span>".$ttax; ?></td>
                    </tr>
                     <?php } elseif($total['label'] == "Total:" || $total['label'] == "Totaal:"){ ?>
                     <tr>
                        <th class="td" scope="row" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo "Totaal incl. BTW"; ?></th>
                        <td class="td" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo $total['value']; ?></td>
                    </tr>
                   <?php }else{ ?>
                    <tr>
                        <th class="td" scope="row" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo $total['label']; ?></th>
                        <td class="td" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo $total['value']; ?></td>
                    </tr><?php }
                }
            }
        ?>
    </tfoot>

如果你想用/,/。分割这样的字符串?然后你可以使用

string='hai, how is your day going? is it good or bad ?'

它将使用&#39;?&#39;符号

或者您可以使用切片方法

string.split('?')

这里0是起始字符索引,4是结束字符索引

string[0:4]