string是表示Unicode字符的字节数组。但是, Python不支持字符数据类型。字符是长度为一的字符串。
例子:
# Python program to demonstrate
# string
# Creating a String
# with single Quotes
String1 = 'Welcome to the Geeks World'
print ( "String with the use of Single Quotes: " )
print (String1)
# Creating a String
# with double Quotes
String1 = "I'm a Geek"
print ( "\nString with the use of Double Quotes: " )
print (String1)
输出如下:
String with the use of Single Quotes:
Welcome to the Geeks World
String with the use of Double Quotes:
I'm a Geek
注意:有关更多信息, 请参阅Python字符串
Collections.UserString
Python支持String就像一个叫做用户字符串存在于收藏夹模块中。此类充当字符串对象的包装器类。当一个人想要创建自己的带有某些修改功能或某些新功能的字符串时, 该类很有用。它可以被视为为字符串添加新行为的一种方式。此类采用任何可以转换为字符串的参数, 并模拟其内容保留在常规字符串中的字符串。该类的data属性可以访问该字符串。
语法如下:
collections.UserString(seq)
范例1:
# Python program to demonstrate
# userstring
from collections import UserString
d = 12344
# Creating an UserDict
userS = UserString(d)
print (userS.data)
# Creating an empty UserDict
userS = UserString("")
print (userS.data)
输出如下:
12344
范例2:
# Python program to demonstrate
# userstring
from collections import UserString
# Creating a Mutable String
class Mystring(UserString):
# Function to append to
# string
def append( self , s):
self .data + = s
# Function to rmeove from
# string
def remove( self , s):
self .data = self .data.replace(s, "")
# Driver's code
s1 = Mystring( "Geeks" )
print ( "Original String:" , s1.data)
# Appending to string
s1.append( "s" )
print ( "String After Appending:" , s1.data)
# Removing from string
s1.remove( "e" )
print ( "String after Removing:" , s1.data)
输出如下:
Original String: Geeks
String After Appending: Geekss
String after Removing: Gkss
注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。