文章首发微信公众号,微信搜索:猿说python
在 python变量 文章中我们对python变量做了一个简单的了解,整数/浮点数/bool值相对来讲都比较简单,今天详细在讲解一下关于字符串的内容,字符串俗称:str。
在本文会大量的使用print 和format 函数,如果还有不太熟悉使用的盆友,请先预习:关于python开发中print 函数和format 函数详细解释
一.字符串运算符
介绍两个关于python字符串的运算符,”in” 和 “not in”,主要用于检测字符串中是否存在某个字符或者字符串,如果存在返回True,不存在返回False,直接上代码演示:
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:何以解忧
@Blog(个人博客地址): shuopython.com
@WeChat Official Account(微信公众号):猿说python
@Github:www.github.com
@File:string123.py
@Time:2019/9/23 20:45
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
# 检测单个字符
str1 = "hello world"
if "h" in str1:
print("{} 字符串包含 'h'".format(str1)) # 注意单引号和双引号的配合使用
else:
print("{} 字符串不包含 'h'".format(str1))
# 检测字符串
if "hello" in str1:
print("{} 字符串包含 'hello'".format(str1)) # 注意单引号和双引号的配合使用
else:
print("{} 字符串不包含 'hello'".format(str1))
# 使用 not in
if "hllo" not in str1:
print("{} 字符串不包含 'hllo'".format(str1)) # 注意单引号和双引号的配合使用
else:
print("{} 字符串包含 'hllo'".format(str1))