Python String
In Python, a string is just a sequence of characters, and they are one of the most widely used data types in the language. It is an immutable data type, so you cannot change it once you create it. In this article, We will explore the various ways of creating, manipulating, and formatting strings in Python.
Creating Strings
In Python, strings can be created using either single or double quotes. For example, the following are both valid ways of creating a string:
ex1 = 'String in single quotes'
ex2 = "String in double quotes"
You can also use triple quotes (single or double) to create a multi-line string. For example:
ex3 = """This is a
multi-line string"""
ex4 = '''This is also
a multi-line string'''
Note
- You can also use backslash(\) with double or single quotes to continue from next line
- Also You Can use str(), a built-in function of Python to create string
String Manipulation
Python has many different built-in functions in the standard library to help you to manipulate strings in many different ways. They can understand different inputs of operators and symbols.
String Concatenation and Repetition
To concatenate two strings, you can use the + operator. For example:
ex1 = "Code"
ex2 = "MyLife"
ex3 = ex1 + ex2
print(ex3)
Output:
CodeMyLife
You can also put more than one quoted constant on aline results in the constants being concatenated. For example:
ex1 = "I" 'am' 'Python'
print(ex1)
Output:
IamPython
You can repeat a string a certain number of times using the * operator. For example:
ex1 = "Hellow"
ex2 = ex1 * 3
print(ex2)
Output:
HellowHellowHellow
Note
- You cannot concatenate numbers to string, it will raise TypeError
String Indexing and Slicing
Python strings are sequences of characters, and each character has an index. The index of the first character is 0, the second is 1, and so on. You can access individual characters of a string using indexing. For example:
ex1 = "Hello"
print(ex1[0])
Output:
H
You can also use negative indexing to access characters from the end of the string. For example:
ex1 = "Hello"
print(ex1[-1])
Output:
o
You can slice a string to extract a substring using the [start:end] notation. The start index is inclusive and the end index is exclusive. For example:
ex1 = "Hello, World!"
print(ex1[7:12])
Output:
World
String Methods
Python provides many built-in methods for manipulating strings. Some of the most commonly used methods are:
Methods | Discription |
---|---|
len(string) | Returns the length of the string |
string.count(value, start, end) | Return number of element with specified value |
string.find(value, start, end) | Return the index number of value in string |
string.join(iterable string) | Join all iterable string using separator string |
string.title() | Returned all word of string with uppercase and remaining letter with lowercase |
string.lower() | Return copy of string converted to lowercase |
string.upper() | Return copy of string converted to uppercase |
string.index(value, start, end) | Return the index number of value where the string is found |
string.split(separator) string.rstrip() string.lstrip() | Return the string with leading and trailing whitespace removed Return the string with trailing whitespace removed Return the string with leading whitespace removed |
string.isalnum() | Return True if characters of string are alphanumeric |
string.isalpha() | Return True if characters of string are alphabetic |
string.islower() | Return True if all characters in string are lowercase |
string.isspace() | Return True if string have any whitespace |
string.isupper() | Return True if all characters in string are upercase |
string.istitle() | Return True if string has tittle case |
string.replace(old,new) | Return copy of string,were old word replaced by new |
string.partition(separator) | Splits the string at the first occurrence of the separator |
string.endswith(value) string.startswith(value) | Return True if ends with the value Return True if start with the value |
string.capitalize() | Return the copy of string with its first character capitalized |
String Formatting
String formatting in Python allows you to insert dynamic values into a string by using placeholders, also known as format specifiers. These placeholders are denoted by curly braces {} and can be replaced with actual values using the format() method or using f-strings (in Python 3.6 and above). Additionally, the format() method can be used with positional arguments, or with keyword arguments. For example:
name = "Abhay"
age = 14
output = "My name is {} and I am {} years old.".format(name, age)
print(output)
Output:
My name is Abhay and I am 25 years old.
String also supports different encodings like utf-8, ASCII, etc.
Overall, strings are a powerful and versatile data type in Python, and they are used in a wide variety of applications, from text processing to web development.
You had included a lot about strings. Really helpful for beginner in programming.