隐蔽战线 發表於 2025-9-28 21:35:00

数据类型-字符串

<p><span style="color: rgba(0, 0, 0, 1)"><strong>字符串(不可变类型):&nbsp; &nbsp;python3&nbsp; &nbsp;默认编码为utf-8&nbsp; &nbsp; 而其中字符串用unicode编码存储在内存</strong></span></p>
<p><span style="color: rgba(0, 0, 0, 1)"><strong> info = ”guohan“&nbsp; &nbsp; &nbsp; info 1= "1"</strong></span></p>
<p><span style="color: rgba(0, 0, 0, 1)"><strong>公共功能</strong>:</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  1.索引:info = ”g“</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  2.切片:info【1:】 = ”uohan“</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  3.步长:info【::2】 = ”goa”</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  4.for循环:for i in info:</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  5.删除:字符串是不可变类型所以其内部字符不可被删</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  6.修改:同理&nbsp; (5,6 由于不可变当有改动时不再是info 而是其他变量接收变成新字符串)</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  7.len:&nbsp; &nbsp; len(info)&gt;&gt;&gt;6</span></p>
<p><strong><span style="color: rgba(0, 0, 0, 1)">独有方法:</span></strong></p>
<p><strong><span style="color: rgba(0, 0, 0, 1)">  </span></strong><span style="color: rgba(0, 0, 0, 1)">1.upper/lower:  v = info.upper()/lower()-&gt; v = "GUOHAN"/"guohan"</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  2.isdecimal(判断是否为十进制数字):  print(info1.isdecimal())-&gt;True &nbsp; &nbsp;若info = “二”则为false</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  3.encode(编码转换):  encode:字符串转二进制&nbsp; info.encode(utf-8)  将info根据utf-8编码转换成二进制</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">              decode:二进制转换成字符&nbsp; 如:v= b“xe4....."  v.decode(utf-8) = ”你好“</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  4.replace(替换):<strong>格式:info.replace(old,new,num</strong>(几次的意思)<strong>) </strong> <span style="color: rgba(186, 55, 42, 1)"><strong>num不指定则是全部替换</strong></span></span></p>
<p><span style="color: rgba(0, 0, 0, 1)">           info= ”guoohan“  info.replace(o,n,1)-&gt;"gunohan"</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  5.split(分割 ):<strong>格式:info.split(指定字符,num)&nbsp; &nbsp; &nbsp; <span style="color: rgba(186, 55, 42, 1)">num为分割次数&nbsp;</span> <span style="color: rgba(186, 55, 42, 1)">分割成列表(里面不含指定字符)</span></strong></span></p>
<p><span style="color: rgba(0, 0, 0, 1)"><strong>         </strong>info = "guo2han"  info.split("2")-&gt;[”guo","han"]</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">         info= ”guo2h2an"&nbsp; &nbsp; info.split("2",1)-&gt;["guo","h2an"]</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  6.partition(将指定字符串分成3份):<span style="color: rgba(186, 55, 42, 1)"><strong>分割成元组(前面,指定字符,后面)</strong></span> &nbsp;info.partition("o")-&gt;('gu','o','han')</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  7.strip(去除<span style="color: rgba(186, 55, 42, 1)"><strong>两边</strong></span>空白,换行符,制表符):  info= “&nbsp; &nbsp; guohan&nbsp; ”  info.strip()-&gt;"guohan"</span></p>
<p><span style="color: rgba(0, 0, 0, 1)">  8.join(连接):  <strong><span style="color: rgba(186, 55, 42, 1)">join括号里面的东西不一定是字符串但.前面必须是</span></strong><span style="color: rgba(186, 55, 42, 1)"><strong>,且最后连接成的东西一定是字符串</strong></span></span></p>
<p><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)">          v = "-"&nbsp; &nbsp;a = ["g","o"]&nbsp; &nbsp;v.join(a)-&gt;"g-o"</span></span></p>
<p><strong><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)">字符串格式化:</span></span></strong></p>
<p> <strong> %占位符:<span style="color: rgba(186, 55, 42, 1)">使用时注意占位符和变量类型匹配</span></strong></p>
<p><span style="color: rgba(186, 55, 42, 1)">  <span style="color: rgba(0, 0, 0, 1)">  %s&gt;&gt;&gt;str  %d&gt;&gt;&gt;int  %f&gt;&gt;&gt;folat  &lt; 左对齐  &gt; 右对齐</span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">    1.输出  name = "guohan"  age = "20"</span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">      &nbsp; &nbsp;print("我是%s,年龄%d"%(name,age))</span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">    2.格式化输出  .nf%保留n位小数</span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)"> <strong> format方法:</strong></span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>   </strong> 1.输出  print("我是{0},年龄{1}".format(name,age))</span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">    2.格式化输出:  {:.nf}  print("{:.2f}".format(1.0/3))&gt;&gt;&gt;0.33</span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">             {:m&lt;nd}   控制数据输出宽度补齐数字  n为宽度,m为补齐的数字左对齐</span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">&nbsp;            &nbsp; {:n%}  用百分号表示后保留n位</span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">  <strong>f-string方法:</strong></span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">    1.输出  print(f“我是{name},年龄{age}")</span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">    2.格式化输出  {num:.nf}  num = 2.3  print(f"{num:.2f}")&gt;&gt;&gt;2.30</span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">            {num:0nd}  宽度n,不足补0   </span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">            s = ”hello“</span></span></p>
<p><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">            print(f”{s:&lt;-10}") 左对齐宽度为10,用-补齐 </span></span></p>
<p><strong><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">字节类型(bytes类型):py3中 使用encode()方法将以unicode编码的字符串按照指定编码进行编码后返回的都是bytes类型</span></span></strong></p>
<p><strong><span style="color: rgba(186, 55, 42, 1)"><span style="color: rgba(0, 0, 0, 1)">  特点:前面有b</span></span></strong></p>
<pre class="language-python highlighter-hljs"><code>v = "郭晗"
v1 = v.encode('utf-8')
print(v1,type(v1))
&gt;&gt;&gt;b'\xe9\x83\xad\xe6\x99\x97' &lt;class 'bytes'&gt;

v = "guohan"
v1 = v.encode('utf-8')
print(v1,type(v1))
&gt;&gt;&gt;b'guohan' &lt;class 'bytes'&gt;</code></pre>
<p>&nbsp;</p>
<p><strong>Python3 严格区分「字符串」和「字节流」,这是和 Python2 最核心的差异之一:</strong></p>
<ul>
<li>Unicode 是<strong>字符集标准</strong>,收录了全球几乎所有人类文明的字符,给每个字符分配了唯一的「身份编号」(码点),是所有字符编码的基础;</li>
<li>UTF-8 是 Unicode 的<strong>一种字节编码实现方案</strong>(还有 UTF-16/UTF-32),核心优势是<strong>可变长字节</strong>,用更少字节存储字符(英文 1 字节、中文 3 字节),适配性最强;</li>
<li>Python3 中 <code>str</code> 类型,本质是<strong>Unicode 字符的序列</strong>,直接存储字符的码点,不是具体字节;</li>
<li>Python3 中 <code>bytes</code> 类型,是<strong>将 Unicode 字符(str)通过某一种编码(如 UTF-8/GBK)转换后得到的字节流</strong>,是计算机能直接识别的二进制数据</li>
</ul>
<p>&nbsp;</p>
<p>字符串类型一般用于内存中做数据操作</p>
<p>字节类型一般用于数据存储和网络传输</p><br><br>
来源:https://www.cnblogs.com/guohan222/p/19117667
頁: [1]
查看完整版本: 数据类型-字符串