Strings are the arrays of bytes representing Unicode characters. However, Python does not support the character data type. A character is a string of length one.
Example:
# 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) |
Output:
String with the use of Single Quotes: Welcome to the Geeks World String with the use of Double Quotes: I'm a Geek
Note: For more information, refer to Python String
Collections.UserString
Python supports a String like a container called UserString present in the collections module. This class acts as a wrapper class around the string objects. This class is useful when one wants to create a string of their own with some modified functionality or with some new functionality. It can be considered as a way of adding new behaviors for the string. This class takes any argument that can be converted to string and simulates a string whose content is kept in a regular string. The string is accessible by the data attribute of this class.
Syntax:
collections.UserString(seq)
Example 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) |
Output:
12344
Example 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) |
Output:
Original String: Geeks String After Appending: Geekss String after Removing: Gkss
Recommended Posts:
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python program to check if the list contains three consecutive common numbers in Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

