首页 > 系统教程 > LINUX > 正文

python之字符串详解

王林
发布: 2024-02-14 17:30:30
转载
959人浏览过

python之字符串详解

1,变量命名

C/C++标识符的命名规则:变量名只能包含字母、数字和下划线,并且不可以以数字打头。不可以使用C/C++的关键字和函数名作为变量名。

变量命名的规则和C/C++标识符的命名规则是类似的:变量名只能包含字母、数字和下划线,并且不可以以数字打头。不可以使用python的关键字和函数名作为变量名。

另外,我们在取名的时候,尽量做到见名知意(具有一定的描述性)。

2.python字符串

在python种,用引号括起来的都是字符串(可以是单引号,也可以是双引号)

立即学习Python免费学习笔记(深入)”;

虽然,字符串可以是单引号,也可以是双引号,但是如果出现混用,可能就会出错,如下例:

"I told my friend,'python is really useful' " #true
'I told my friend,"python is really useful" ' #true
'I told my friend,'python is really useful' '  #false
登录后复制

一般情况下,我们的集成开发环境会对于我们写的代码进行高亮显示的,应该写完之后根据颜色就可以看出来错误(但是不是所有人都能看出来的),如果没看出来,可以在编译的时候,发现报错如下:

SyntaxError: invalid syntax
登录后复制

这时候,我们就应该去检查一下,是不是在代码中引号混用等。

3,字符串方法总结
(1)每个单词的首字母大写的title()方法
str = "The best makeup is a smile."
print( str )
print( str.title() )
print( str )
登录后复制

输出如下:

The best makeup is a smile.
The Best Makeup Is A Smile.
The best makeup is a smile.
登录后复制
登录后复制
登录后复制

总结,通过这个例子,这可以看出来title()方法是暂时的,并没有更改原来字符串的值。

(2)将字符串变为全大写的upper()方法
str = "The best makeup is a smile."
print( str )
print( str.upper() )
print( str )
登录后复制

输出如下:

The best makeup is a smile.
The Best Makeup Is A Smile.
The best makeup is a smile.
登录后复制
登录后复制
登录后复制

总结,通过这个例子,这可以看出来upper()方法是暂时的,并没有更改原来字符串的值。

(3)将字符串变为全小写的lower()方法
str = "The best makeup is a smile."
print( str )
print( str.lower() )
print( str )
登录后复制

输出如下:

The best makeup is a smile.
The Best Makeup Is A Smile.
The best makeup is a smile.
登录后复制
登录后复制
登录后复制

总结,通过这个例子,这可以看出来lower()方法是暂时的,并没有更改原来字符串的值。

(4)合并字符串

python使用“ + ”号来合并字符串。
例如:

str = "The best makeup is a smile."
print( "He said that "+str.lower() )
登录后复制

输出如下:

He said that the best makeup is a smile.
登录后复制
(5)删除字符串前端空白的lstrip()方法

例如:

str = "    The best makeup is a smile."
print( str )
print( str.lstrip() )
print( str )
登录后复制

输出如下:

    The best makeup is a smile.
The best makeup is a smile.
The best makeup is a smile.
登录后复制

总结,通过这个例子,这可以看出lstrip()方法是暂时的,并没有更改原来字符串的值。

(6)删除字符串后端空白的rstrip()方法

例如:

str = "    The best makeup is a smile.    "
print( str )
print( str.rstrip() )
print( str )
登录后复制

输出如下:

"    The best makeup is a smile.    "
"    The best makeup is a smile. "
"    The best makeup is a smile.    "
登录后复制

总结,通过这个例子,这可以看出rstrip()方法是暂时的,并没有更改原来字符串的值。

(7)删除字符串两端空白的strip()方法

例如:

str = "    The best makeup is a smile.    "
print( str )
print( str.strip() )
print( str )
登录后复制

输出如下:

" The best makeup is a smile. "
"The best makeup is a smile."
" The best makeup is a smile. "
登录后复制

总结,通过这个例子,这可以看出strip()方法是暂时的,并没有更改原来字符串的值。

看到这里,你估计想问,那我如何更改字符串的值呢?只需要将更改过后的值再写回原来的字符串就可以了。

下面我们来举一个例子:

str = "The best makeup is a smile."
print( str )
str = str.title()
print( str )
登录后复制

输出如下:

The best makeup is a smile.
The Best Makeup Is A Smile.
登录后复制

好啦,今天的字符串总结先到这里,如果有疑问,欢迎留言。

以上就是python之字符串详解的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:Linux就该这么学网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号