Collections.UserString in Python

2024 年 8 月 29 日 | 阅读 2 分钟

字符串 是表示 Unicode 字符的字节。字符是长度为一的字符串。问题是 Python 不支持这种数据类型字符。

示例

输出

String with the use of Single Quotes: 
JavaTpoint is the best platform to learn Python

 String with the use of Double Quotes: 
JavaTpoint is the best platform to learn Python

Collections.UserString

Python 在 collections 模块中提供了一个字符串容器,称为 **UserString**。该类作为包装字符串对象的附加类。这个类在创建自定义字符串(已修改或具有新功能的字符串)时很有用。可以为字符串创建新功能。该类可用于接受任何可转换为字符串的参数,并创建一个模拟的非结构化字符串,其内容存储在常规字符串中。可以通过其 data 属性访问字符串。

语法

UserString 的语法是

示例 1:(用户字典,带值和空)

输出

UserString 1:  123546
UserString :  

示例 2:可变字符串(Append 函数和 Remove 函数)

输出

The Original String:  JavaTpoint
String After Appending:  JavaTpointing
String after Removing:  JvTpointing

结论

在本教程中,我们讨论了如何在 Python 中使用 "collections" 模块的 UserString 函数。