python之字符串详解

Crq
Crq
管理员
1871
文章
0
粉丝
Linux教程评论71字数 1009阅读3分21秒阅读模式
摘要大多数人学习的第一门编程语言是C/C++,个人觉得C/C++也许是小白入门的最合适的语言,但是必须承认C/C++确实有的地方难以理解,初学者如果没有正确理解,就可能会在使用指针等变...
1,变量命名

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

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

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

2.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.

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

weinxin
我的微信
微信号已复制
我的微信
这是我的微信扫一扫
 
Crq
  • 本文由 Crq 发表于2024年10月18日 05:17:59
  • 转载请注明:https://www.cncrq.com/11240.html
【技术快报】9.12-9.18 Linux教程

【技术快报】9.12-9.18

本期《linux就该这么学》的技术周报中,将为您推出Nginx源码安装及调优配置、国内三大云数据库测试对比、12个Linux进程管理命令介绍、怎样在Ubuntu中修改默认程序、在a...
最牛X的GCC 内联汇编 Linux教程

最牛X的GCC 内联汇编

正如大家知道的,在C语言中插入汇编语言,其是Linux中使用的基本汇编程序语法。本文将讲解 GCC 提供的内联汇编特性的用途和用法。对于阅读这篇文章,这里只有两个前提要求,很明显,...
10款优秀Vim插件帮你打造完美IDE Linux教程

10款优秀Vim插件帮你打造完美IDE

如果你稍微写过一点代码,就能知道“集成开发环境”(IDE)是多么的便利。不管是Java、C还是Python,当IDE会帮你检查语法、后台编译,或者自动导入你需要的库时,写代码就变得...
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证