码迷,mamicode.com
首页 > 编程语言 > 详细

python--字符串

时间:2017-07-24 13:29:06      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:UI   python   bootstra   char   www   height   image   blank   css   

Python 不支持单字符类型,单字符也在Python也是作为一个字符串使用。

str = "hello jiao";


# 从索引0开始到index=-1之前(包含index=0不包含index=-1【index=-1是字符串最后一个字符】)
print(str[0: -1]);
# hello jia



# 从索引0开始到index=4之前(包含index=0不包含index=4)
print(str[0: 4]);
print(str[: 4]);
# hell



# 从索引6开始到结尾(包含index=6)
print(str[6: ]);
# jiao



# 输出两次字符串
print(str * 2);



# 字符串拼接
print(str[0: 5] + "  world!!!");

 

技术分享

字符串中空格不会省略。

转义字符有效

字符串方法只会返回修改后的字符串,原字符串变量不会被改变(即不能修改字符串中的某个字符,报错)

 

字符串赋值

str = "hello";

str += " jiao";
print(str);     #hello jiao

str = "jiao";
print(str);   #jiao

 

 

字符串运算符

技术分享

 

 

三引号

允许一个字符串跨多行(所见即所得)

print("""
    <meta charset="utf-8" />
        <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

        <link rel="stylesheet" href="//cdn.bootcss.com/uikit/2.25.0/css/uikit.css" />
        <script src="//cdn.bootcss.com/uikit/2.25.0/js/uikit.js"></script>

        <link href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
        <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>

        <link rel="stylesheet" href="css/global.css">
        <link rel="stylesheet" href="css/index.css">

        <script src="js/main.js"></script>
""");

 

字符串内建函数

技术分享

 

 

参考:Python3 字符串

 

python--字符串

标签:UI   python   bootstra   char   www   height   image   blank   css   

原文地址:http://www.cnblogs.com/jiaoxuanwen/p/7227995.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!