当Python字符串遇上MySQL

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 学习的时候我喜欢对比,MySQL和Oracle比,Python和MySQL比,总能有一些收获,也有了新的理解。 今天整理这部分内容的时候,我发现Python和MySQL还是有很多相似之处。

学习的时候我喜欢对比,MySQL和Oracle比,Python和MySQL比,总能有一些收获,也有了新的理解。

今天整理这部分内容的时候,我发现Python和MySQL还是有很多相似之处。学习一门语言,一个数据库,字符串的处理都是一个相对重要的部分,所以我决定对比一下两者的差别。

下面的演示会一边Python,一边MySQL,所以按照这个思路来看就不会感觉突兀了。

转义字符

>>> print '\\'
\

mysql> select '\\';
+---+
| \ |
+---+
| \ |
+---+

>>> print '\"'
"
mysql> select '\"';
+---+
| " |
+---+
| " |
+---+

>>> print '\''
'
mysql> select '\'';
+---+
| ' |
+---+
| ' |
+---+


字符串拼接
>>> x = 'hello'
>>> y = 'tester'
>>> z = x + y
>>> print z
hellotester

set @x='hello';
set @y='tester';
mysql> select @x;
+-------+
| @x    |
+-------+
| hello |
mysql> select @y;
+--------+
| @y     |
+--------+
| tester |
+--------+
mysql> select concat(@x,@y);
+---------------+
| concat(@x,@y) |
+---------------+
| hellotester   |
+---------------+


字符串复制
>>> print '#'*20
####################
mysql> select repeat('#',20);
+----------------------+
| repeat('#',20)       |
+----------------------+
| #################### |
+----------------------+

>>> print ' '*20 + 'end'
                    end
mysql> select space(20);
+----------------------+
| space(20)            |
+----------------------+
|                      |
+----------------------+
                    

字符串截取
>>> name = 'yangjianrong'
>>> name[0]
'y'

>>> name[-1]
'g'
>>> name[1]
'a'
>>> name[1:4]
'ang'

>>> name[:]
'yangjianrong'
>>>
>>> name[1:4:2]
'ag'

mysql> set @name:='yangjianrong';
mysql> select left(@name,1);         
+---------------+
| left(@name,1) |
+---------------+
| y             |
+---------------+
mysql> select right(@name,1);
+----------------+
| right(@name,1) |
+----------------+
| g              |
+----------------+
mysql> select substring(@name,2,3);
+----------------------+
| substring(@name,2,3) |
+----------------------+
| ang                  |
+----------------------+
mysql> select substring(@name,1);
+--------------------+
| substring(@name,1) |
+--------------------+
| yangjianrong       |
+--------------------+
或者使用mid
mysql> select mid(@name,2,3);         
+----------------+
| mid(@name,2,3) |
+----------------+
| ang            |
+----------------+
mysql>  select mid(@name,1);         
+--------------+
| mid(@name,1) |
+--------------+
| yangjianrong |
+--------------+

>>> name
'yangjianrong'

>>> print '%s' %name
yangjianrong


字符串格式化,匹配
>>> '{name},{alias}'.format(name='yangjianrong',alias='jeanron100')    
'yangjianrong,jeanron100'
>>>

mysql>  select concat(insert(@name,1,4,'yangjianrong'),insert(@alias,1,5,'jeanron100')) comm;
+------------------------+
| comm                   |
+------------------------+
| yangjianrongjeanron100 |
+------------------------+


字符串长度>>> ba
'this is a test bar'
>>> len(ba)
18
mysql> select length(@ba);


字符串空格处理

>>> s = ' abc '
>>> s.lstrip()
'abc '
>>> s.rstrip()
' abc'
>>> s.strip()
'abc'
>>>
mysql> set @s=' abc ';
Query OK, 0 rows affected (0.00 sec)
mysql> select ltrim(@s);
+-----------+
| ltrim(@s) |
+-----------+
| abc       |
+-----------+
1 row in set (0.00 sec)

mysql> select rtrim(@s);
+-----------+
| rtrim(@s) |
+-----------+
|  abc      |
+-----------+
1 row in set (0.00 sec)

mysql> select trim(@s);
+----------+
| trim(@s) |
+----------+
| abc      |
+----------+
1 row in set (0.00 sec)

字符串匹配
>>> l = ['a','b','c']
>>> ''.join(l)
'abc'
>>> '*'.join(l)
'a*b*c'

mysql> select concat_ws(',','a','b','c','d','e') comm;
+-----------+
| comm      |
+-----------+
| a,b,c,d,e |
+-----------+


>>> s = 'a b c d e '
>>> s.split(' ')
['a', 'b', 'c', 'd', 'e', '']
mysql> set @s='a b c d e ';
Query OK, 0 rows affected (0.00 sec)

mysql> select replace(@s,' ',',');
+---------------------+
| replace(@s,' ',',') |
+---------------------+
| a,b,c,d,e,          |
+---------------------+


字符串复制
>>> s = 'aabbcc'
>>> s.replace('aa','tt')
'ttbbcc'

mysql> set @s='aabbcc';
Query OK, 0 rows affected (0.00 sec)

mysql> select replace(@s,'aa','tt');
+-----------------------+
| replace(@s,'aa','tt') |
+-----------------------+
| ttbbcc                |
+-----------------------+

 字符串编码
>>> s.encode('utf8')
'aabbcc'

mysql> select  convert(@s using utf8);
+------------------------+
| convert(@s using utf8) |
+------------------------+
| aabbcc                 |
+------------------------+


判断字符串开始匹配的字符
>>> s.startswith('aa')
True

mysql> SELECT LOCATE('aa',@s,1);  
+-------------------+
| LOCATE('aa',@s,1) |
+-------------------+
|                 1 |
+-------------------+

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
12天前
|
SQL 存储 关系型数据库
|
23小时前
|
数据处理 Python
Python中按指定数量分割列表字符串的方法
Python中按指定数量分割列表字符串的方法
6 1
|
5天前
|
关系型数据库 MySQL Linux
【MySQL-10】数据库函数-案例演示【字符串/数值/日期/流程控制函数】(代码演示&可cv代码)
【MySQL-10】数据库函数-案例演示【字符串/数值/日期/流程控制函数】(代码演示&可cv代码)
【MySQL-10】数据库函数-案例演示【字符串/数值/日期/流程控制函数】(代码演示&可cv代码)
|
7天前
|
存储 算法 Python
【亮剑】如何在 Python 中查找两个字符串之间的差异位置?
【4月更文挑战第30天】本文探讨了Python中查找两个字符串差异位置的方法。首先,通过内置函数和基本字符串操作,可以逐个字符比较找到第一个不同位置。其次,利用`difflib`库的`SequenceMatcher`能获取更详细的差异信息。最后,通过实现Levenshtein距离算法,可以计算字符串间的最小编辑距离。根据需求选择合适的方法,能提升代码效率和可读性。
|
7天前
|
SQL 关系型数据库 MySQL
使用Python的pymysql库连接MySQL,执行CRUD操作
使用Python的pymysql库连接MySQL,执行CRUD操作:安装pymysql,然后连接(host='localhost',user='root',password='yourpassword',database='yourdatabase'),创建游标。查询数据示例:`SELECT * FROM yourtable`;插入数据:`INSERT INTO yourtable...`;更新数据:`UPDATE yourtable SET...`;删除数据:`DELETE FROM yourtable WHERE...`。
16 0
|
8天前
|
SQL 关系型数据库 MySQL
Python操作mysql数据库
Python操作mysql数据库
|
8天前
|
Go 索引 Python
非常全面的python字符串相关处理方法(二)
非常全面的python字符串相关处理方法(二)
|
8天前
|
存储 索引 Python
非常全面的python字符串相关处理方法(一)
非常全面的python字符串相关处理方法(一)
|
11天前
|
人工智能 索引 Python
Python 字符串格式化输出
Python 字符串格式化输出
8 0
|
11天前
|
Python
Python 字符串格式化指南
本文介绍了Python中的三种字符串格式化方法:1) 使用 `%` 操作符,如 `"%s %d" % (var1, var2)`;2) `str.format()` 方法,如 `"{} {}".format(var1, var2)`;3) Python 3.6+ 的 f-strings,如 `f"{var1} {var2}"`。每种方法都支持变量插入和格式控制,如指定小数位数。选择合适的方法能提升代码可读性和效率。
9 0