zoukankan      html  css  js  c++  java
  • Python学习(三) 输出任意格式的字符串以及字符串的切片

    在Python中想要输出一句话,如下

     1 a='hello world'
     2 print a
     3 //打印出的是hello world
     4 
     5 print 'hello 
     world'
     6 //打印出的是 
     7 //hello
     8 //world
     9 print '''hello
    10     world
    11     good 
    12     bye'''
    13 //打印出的是
    14 //hello
    15 //world
    16 //good
    17 //bye

       如果想要输出换行的字符串,可以再字符串中添加转义字符 ' ',或者使用''' 或"""将有格式输出的字符串包裹起来。

       另外''' 或者""" 还有多行注释的作用。

      字符串的切片

      

     1 str='abcde'
     2 print a[0]
     3 //输出a
     4 print a[1]
     5 //输出b
     6 print a[0]+a[1]
     7 //输出ab
     8 
     9 切片:
    10 print str[1:4]
    11 //输出bcd
    12 print str[1:]
    13 //输出bcde
    14 print str[1::2]
    15 //输出bd
    16 print str[-1:-4:-1]
    17 //输出edc
    18 
    19 str[x:y:z] 其中str是字符串 ,x 是切片起始点,y是切片终点,z是步长
    20 
    21 所以用Python很容易实现字符串的反转
    22 print str[-1:-6:-1]
    23 //输出edcba
  • 相关阅读:
    20150212-2015
    SM30维护视图添加按钮
    SAP保存操作记录CDHDR和CDPOS表
    20150123-慢慢
    20150124-轻轻
    维护 物料主数据 号码段
    ABAP DEMO-2018
    工具
    幽门螺杆菌资料收集
    MySQL 8 连接时出现 1251 和 2059 错误
  • 原文地址:https://www.cnblogs.com/nihousheng/p/4545092.html
Copyright © 2011-2022 走看看