Python String

Python String Tutorial

Introduction:

A string is a sequence of characters. In Python, you can define a string using either single quotes () or double quotes (), or triple quotes (”’ or “””) for multiline strings. Here are some examples:

				
					my_string_1 = "Hello world"
my_string_2 = 'sqatools'
my_string_3 = '''In Python, strings are a fundamental and 
versatile data type that plays a crucial role in text
processing and manipulation. Python strings are sequences 
of characters, and they offer a plethora of built-in 
methods to perform various operations on text data. 
You can use Python string methods to find the length of a 
string,concatenate multiple  strings, convert cases, split 
strings into lists, and replace substrings. The string 
format method allows you to create dynamic and readable
output by embedding variables within a formatted string.'''

#Printing type of string
print( type ( my_string_1 ) ) # Output : < class ‘str’ >
print( type ( my_string_2 ) ) # Output : < class ‘str’ >
print( type ( my_string_3 ) ) # Output : < class ‘str’ >

				
			

Python string features:

  1. Immutable: Strings in Python are immutable, meaning that once a string is created, its contents cannot be changed. If you want to modify a string, you need to create a new string with the desired changes.
  2. Sequence of Characters: Strings are a sequence of individual characters. Each character in a string is assigned an index, starting from 0 for the first character. This allows you to access and manipulate specific characters within a string using indexing and slicing operations.
  3. Unicode Support: Python strings support Unicode, allowing you to work with characters from different languages, including non-ASCII characters. This enables you to handle and process text in various encodings and writing systems.
  4. Concatenation: Strings can be concatenated using the “+” operator, allowing you to combine multiple strings into a single string. For example, “Hello” + ” ” + “World” results in the string “Hello World”.
  5. Length: You can obtain the length of a string using the `len()` function, which returns the number of characters in the string. This is useful for determining the size of a string or iterating over its characters.
  6. String Formatting: Python provides various methods for formatting strings. This includes the use of placeholders or format specifiers, such as `%` operator or the `format()` method, to insert variables or values into a string with specified formatting.
  7. String Operations and Methods: Python offers a wide range of built-in operations and methods specifically designed for working with strings. These include operations like string concatenation, repetition, and membership checks, as well as methods for case conversion, substring searching, replacing, splitting, stripping, and more.
  8. Escape Sequences: Strings in Python support escape sequences, which allow you to include special characters that are difficult to type directly. For example, using `\n` represents a newline character, `\t` represents a tab character, and `\”` represents a double quote character within a string.
  9. String Comparison: Strings can be compared using operators such as `==`, `!=`, `<`, `>`, `<=`, and `>=`. Comparison is performed based on lexicographic (dictionary) ordering of characters, which compares each corresponding character’s Unicode value.
  10. String Iteration: Python allows you to iterate over a string using loops, enabling you to process each character individually or perform operations on the entire string.

Python string advantages:

  1. Versatility: Python strings are versatile and widely used for handling and manipulating text-based data. They can store and process alphanumeric characters, symbols, and even Unicode characters, making them suitable for various applications and internationalization needs.
  2. Immutable Nature: Python strings are immutable, meaning their contents cannot be changed once they are created. This immutability ensures the integrity of strings and prevents accidental modifications, making them reliable for operations like string matching, hashing, or as keys in dictionaries.
  3. Extensive String Methods: Python provides a rich set of built-in string methods that offer a wide range of operations. These methods allow you to perform tasks like searching, replacing, splitting, joining, formatting, case conversion, and more. The extensive set of string methods simplifies complex string manipulation tasks and saves you time and effort.
  4. Unicode Support: Python strings have excellent support for Unicode, making them capable of representing characters from various writing systems, including non-ASCII characters. This allows you to work with and process text in different languages and encodings, making Python strings suitable for internationalization and multilingual applications.
  5. String Formatting: Python offers flexible and powerful string formatting capabilities. With techniques like the `%` operator or the `format()` method, you can easily insert variables or values into a string with specified formatting. This makes it convenient for generating dynamic strings, constructing messages, or formatting output.
  6. String Interpolation: Python 3.6 and later versions introduce f-strings, which provide a concise and readable way to embed expressions within string literals. This feature, known as string interpolation, simplifies the process of combining variables and expressions with strings, improving code readability and reducing the need for concatenation.
  7. Compatibility with File Operations: Python strings integrate seamlessly with file handling operations. You can read or write strings to files, manipulate file paths as strings, and use string methods to parse or manipulate file content. This compatibility makes it easier to work with text files and perform file-related operations.
  8. Regular Expressions: Python’s `re` module offers support for regular expressions, providing powerful pattern matching and text manipulation capabilities. Regular expressions allow you to search for specific patterns, extract information, or perform complex text transformations, making them valuable for tasks like data validation, parsing, and text processing.
  9. Consistency and Familiarity: Python strings follow consistent syntax and behavior across different operations and methods. This consistency, combined with Python’s focus on readability and simplicity, makes working with strings in Python intuitive and easy for developers who are familiar with the language.
  10. Extensive Community and Documentation: Python is a widely adopted programming language, and as a result, there is a vast community of developers and extensive documentation available for working with strings. You can find libraries, tutorials, and resources to help with advanced string manipulation, handling special cases, or optimizing string-related operations.

Python string disadvantages:

  1. Immutability: While immutability can be an advantage in terms of data integrity, it can also be a disadvantage when it comes to efficiency. Since strings are immutable, any operation that involves modifying a string, such as concatenation or replacing characters, requires creating a new string object. This can be inefficient, especially when dealing with large strings or performing many operations.
  2. Memory Overhead: Each individual character in a Python string requires memory to store its Unicode value and other metadata. This fixed overhead per character can consume significant memory, especially when working with large strings or processing large amounts of text data. In scenarios with memory constraints, this overhead can become a limiting factor.
  3. Difficulty with Mutable Operations: Some string manipulation tasks, such as inserting or removing characters at specific positions, can be cumbersome and inefficient due to the immutability of strings. Achieving such operations often requires creating new string objects or converting the string to a mutable data structure like a list, performing the operation, and then converting it back to a string.
  4. Comparison Complexity: Comparing strings for equality or sorting them can have time complexity proportional to the length of the strings being compared. In cases where string comparison or sorting is a critical performance factor, more efficient data structures or algorithms, like tries or radix sort, might be more suitable.
  5. Limited Unicode Support in Older Versions: While Python has excellent Unicode support, there were limitations in earlier versions (pre-Python 3) regarding Unicode handling. Older versions required explicit encoding and decoding operations when working with non-ASCII characters, which could be a source of confusion or errors.
  6. Limited Mutable String Options: While Python strings are immutable, there may be scenarios where mutable string operations are desired. Python’s built-in string methods offer limited options for in-place modification, and workarounds like converting the string to a list and back can introduce additional complexity and potential performance drawbacks.
  7. Difficulty with Low-Level Operations: Python strings abstract away low-level memory access and manipulation, which can be advantageous for most applications. However, in certain scenarios where direct byte-level or bit-level manipulation is required, Python strings may not be the most suitable data structure, and using other byte-oriented data types or libraries may be more appropriate.
  8. Performance in Heavy String Manipulation: For scenarios that involve heavy string manipulation, such as text parsing or processing large volumes of text data, Python strings may not offer the best performance. In such cases, lower-level languages or specialized text processing libraries may provide more efficient options.

Re-assign Strings:

In Python, you can reassign a string to a new value by simply assigning the new value to the variable that holds the string.  Here’s an example:

				
					my_string = "Hello, World!"
print( my_string )  # Output : Hello, World!
my_string = "Sqatools"
print( my_string )  # Output : Sqatools

				
			

In the example above, the first print statement outputs “Hello, World!” because my_string is initially assigned that value. Then, my_string is reassigned to “Sqatools” and the second print statement outputs the new value.

It’s important to note that strings are immutable in Python, which means that once you create a string, you cannot modify it. When you “modify” a string by, for example, concatenating two strings or removing a character, you are actually creating a new string object. Here are some examples:

				
					my_string = "Hello, World!"
new_string = my_string + "Today is sunday"
print( new_string )  # Output : Hello, World! Today is sunday
my_string = "Hello, World!"
new_string = my_string.replace( "W", "H" )
print( new_string )  # Output : Hello, Horld!

				
			

Deleting string:

In Python, you cannot delete individual characters in a string because strings are immutable. However, you can delete the entire string object from memory using the del statement.  Here’s an example:

				
					my_string = "Hello, World!"
print( my_string )  # Output : Hello, World!
del my_string
print( my_string )  # Output : NameError: name 'my_string' is not defined

				
			

In the example above, the first print statement outputs the value of my_string, which is “Hello, World!”. Then, the del statement deletes the my_string object from memory. When we try to print my_string again, we get a NameError because the variable no longer exists.

String formatting methods:

Python provides several methods for formatting strings. Here are the most commonly used methods:

  1. The % operator: This operator allows you to format strings using placeholders, which are replaced with values at runtime. Here’s an example:
				
					name = "Ketan"
age = 25
print("My name is %s and I am %d years old" % (name, age))
# Output: My name is Ketan and I am 25 years old

				
			

In the example above, %s and %d are placeholders for the name and age variables, respectively. The values of these variables are provided in a tuple that follows the % operator.

  1. The str.format() method: This method allows you to format strings using placeholders that are enclosed in curly braces. Here’s an example:
				
					name = "Ketan"
age = 25	
print("My name is {} and I am {} years old".format(name, age))
# Output: My name is Ketan and I am 25 years old

				
			

In the example above, {} is a placeholder for the name and age variables. The values of these variables are provided as arguments to the str.format() method.

  1. F-strings: It allows you to embed expressions inside placeholders that are enclosed in curly braces preceded by the f character. Here’s an example:
				
					name = "Ketan"
age = 25
print(f"My name is {name} and I am {age} years old")
# Output: My name is Ketan and I am 25 years old

				
			

In the example above, the f character before the string indicates that it is an f-string. The expressions inside the curly braces are evaluated at runtime and the resulting values are inserted into the string.

String operator:

In Python, strings support several operators that can be used to perform various operations on strings. Here are some of the most commonly used string operators:

  1. Concatenation (+): The + operator can be used to concatenate two or more strings. Here’s an example:
				
					Var1 = "Hello"
Var2 = "world"
message = Var1+ Var2
print(message)  # Output: Hello world

				
			
  1. Repetition (*): The * operator can be used to repeat a string a certain number of times. Here’s an example:
				
					line = ‘10’ * 10
print(line)  # Output: 10101010101010101010

				
			
  1. Membership (in): The in operator can be used to check if a substring exists within a string. It returns True if the substring is found, and False otherwise. Here’s an example:
				
					text = "Sqatools is best for learning python"
print("python" in text)  # Output: True
print("BEST" in text)  # Output: False

				
			
  1. Indexing ([]): The [] operator can be used to access individual characters within a string. You can use a positive index to access characters from the beginning of the string, and a negative index to access characters from the end of the string. Here’s an example:
				
					text = "Hello World"
print(text[0])  # Output: H
print(text[-1])  # Output: d

				
			
  1. Slicing ([start:end]): The [] operator can also be used to extract a substring (or slice) from a string. The start argument specifies the starting index of the slice (inclusive), and the end argument specifies the ending index of the slice (exclusive). Here’s an example:
				
					text = "Hello World"
print(text[0:5])  # Output: Hello

				
			
  1. Comparison (==, !=, <, <=, >, >=): The comparison operators can be used to compare two strings alphabetically. The == operator returns True if the two strings are equal, and False otherwise. The != operator returns True if the two strings are not equal, and False otherwise. The < and > operators return True if the left string is alphabetically less than or greater than the right string, respectively. The <= and >= operators return True if the left string is alphabetically less than or equal to or greater than or equal to the right string, respectively. Here’s an example:
				
					word1 = "apple"
word2 = "banana"
print(word1 == word2)  # Output: False
print(word1 < word2)  # Output: True

				
			

String indexing and slicing:

Indexing:

Indexing: Each character in a string is assigned an index starting from 0. You can access a particular character in a string using its index. Here’s an example:

				
					my_string = "Hello World"
print( my_string[ 0 ] )   # Output : H
print( my_string[ 2 ] )   # Output : l
print( my_string[ -1 ] )  # Output : d

				
			
Slicing:

You can extract a portion of a string using slicing. Slicing is done using the [] operator, with two indices separated by a colon (:) inside the brackets. Here’s an example:

				
					my_string = "Hello World"
print( my_string[ 0 : 5] )  # Output : Hello
print( my_string[ : 5 ] )   # Output : Hello
print( my_string[ -5 : ] ) # Output : World

				
			

In the example above, my_string[0:5] returns the first five characters of the string, my_string[:5] returns all the characters up to the fifth character, and my_string[-5:] returns the characters from the fifth-last to the last character.

Note that the first index in a slice is inclusive and the second index is exclusive. So, my_string[0:5] returns characters from index 0 to index 4, but not the character at index 5.

In addition to the two indices separated by a colon, you can also add a third index to specify the step value. Here’s an example:

				
					my_string = "Hello World!"
print( my_string[ :: 2 ] )  # Output : HloWrd

				
			

String functions:

  1. `len()`: Returns the length of a string. Here’s an example:
				
					text = "Hello World"
print(len(text)) # Output: 11

				
			
  1. `lower()`: Converts all characters in a string to lowercase. Here’s an example:
				
					text = "Hello World"
print(text.lower()) # Output: hello world

				
			
  1. `upper()`: Converts all characters in a string to uppercase. Here’s an example:
				
					text = "Hello World"
print(text.upper()) # Output: HELLO WORLD

				
			
  1. `title()`: Capitalizes the first character of each word in a string. Here’s an example:
				
					text = "hello world"
print(text.title()) # Output: Hello World

				
			
  1. `capitalize()`: Capitalizes the first character of a string. Here’s an example:
				
					text = "hello world"
print(text.capitalize()) # Output: Hello world

				
			
  1. `swapcase()`: Swaps the case of all characters in a string. Here’s an example:
				
					text = "Hello World"
print(text.swapcase()) # Output: hELLO wORLD

				
			
  1. `count()`: Returns the number of occurrences of a substring in a string. Here’s an example:
				
					text = "Hello World"
print(text.count("l")) # Output: 3

				
			
  1. `find()`: Returns the index of the first occurrence of a substring in a string. Here’s an example:
				
					text = "Hello World"
print(text.find("e")) # Output: 1

				
			
  1. `rfind()`: Returns the index of the last occurrence of a substring in a string.
				
					text = "Hello World"
print(text.rfind("o")) # Output: 7

				
			

       10. `index()`: Like `find()`, but raises a `ValueError` if the substring is not found.

				
					text = "Hello World"
print(text.index("o")) # Output: 4	
print(text.index("a")) # Raises ValueError: substring not found

				
			
  1. `rindex()`: Like `rfind()`, but raises a `ValueError` if the substring is not found.
				
					text = "Hello, World!"
print(text.rindex("o")) # Output: 8
print(text.rindex("a")) # Raises ValueError: substring not found

				
			
  1. `startswith()`: Returns `True` if a string starts with a specified prefix, otherwise `False`.
				
					text = "Hello World"
print(text.startswith("H")) # Output: True
print(text.startswith("h")) # Output: False

				
			
  1. `endswith()`: Returns `True` if a string ends with a specified suffix, otherwise `False`.
				
					text = "Hello, World!"
print(text.endswith("!")) # Output: False
print(text.endswith("d")) # Output: True

				
			
  1. `replace()`: Replaces all occurrences of a substring with another substring.
				
					text = "Hello World"
print(text.replace("World", "Sqatools")) # Output: Hello Sqatools

				
			

       15. `strip()`: Removes whitespace (or other characters) from the beginning and end of a string.

				
					text = "  Hello, World!   "
print(text.strip()) # Output: Hello World

				
			
  1. `rstrip()`: Removes whitespace (or other characters) from the end of a string.
				
					text = "  Hello World   "
print(text.rstrip()) # Output:   Hello World

				
			
  1. `lstrip()`: Removes whitespace (or other characters) from the beginning of a string.
				
					string = "   Hello World   "
new_string = string.lstrip()
print(new_string) # Output: "Hello World   "

				
			
  1. `split()`: Splits a string into a list of substrings using a specified delimiter.
				
					string = "yash,sanket,omkar"
name = string.split(",")
print(name) # Output: [‘yash’, ‘sanket’, ‘omkar’]

				
			
  1. `rsplit()`: Splits a string from the right into a list of substrings using a specified delimiter.
				
					string = "apple,banana,orange"
fruits = string.rsplit(",", 1)
print(fruits) # Output: ['apple,banana', 'orange']

				
			

The separator used is the comma, which is passed as an argument to the rsplit() method. The 1 argument is used to specify that only one splitting should occur, which in this case splits the last fruit from the first two. Finally, we print the list of fruits.

       20. `join()`: Joins a list of strings into a single string using a specified delimiter.

				
					name = [‘yash’, ‘sanket’, ‘omkar’]
separator = ','
string = separator.join(name)
print(string) ) # Output: "yash, sanket, omkar"

				
			
  1. `isalnum()`: Returns `True` if a string contains only alphanumeric characters, otherwise `False`.
				
					string1 = "Hello123"
string2 = "Hello 123"
print(string1.isalnum()) # Output: True
print(string2.isalnum()) # Output: False

				
			
  1. `isalpha()`: Returns `True` if a string contains only alphabetic characters, otherwise `False`.
				
					string1 = "Hello"
string2 = "Hello123"
print(string1.isalpha()) # Output: True
print(string2.isalpha()) # Output: False

				
			
  1. `isdigit()`: Returns `True` if a string contains only digits, otherwise `False`.
				
					string1 = "123"
string2 = "Hello123"
print(string1.isdigit()) # Output: True
print(string2.isdigit()) # Output: False

				
			
  1. `islower()`: Returns `True` if all characters in a string are lowercase, otherwise `False`.
				
					string1 = "hello"
string2 = "Hello"
print(string1.islower()) # Output: True
print(string2.islower()) # Output: False

				
			

      25. `isupper()`: Returns `True` if all characters in a string are uppercase, otherwise `False`.

				
					string1 = "HELLO"
string2 = "Hello"
print(string1.isupper()) # Output: True
print(string2.isupper()) # Output: False

				
			
  1. `istitle()`: Returns `True` if a string is titlecased (i.e., the first character of each word is capitalized), otherwise `False`.
				
					string1 = "Hello World"
string2 = "Hello world"
print(string1.istitle()) # Output: True
print(string2.istitle()) # Output : False

				
			
  1. `isspace()`: Returns `True` if a string contains only whitespace characters, otherwise `False`.
				
					string1 = "   "
string2 = "Hello World"
print(string1.isspace()) # Output: True
print(string2.isspace()) # Output: False

				
			
  1. `maketrans()`: Creates a translation table to be used with the `translate()` function.
				
					string = "hello world"
translation_table = str.maketrans("l", "1")
new_string = string.translate(translation_table)
print(new_string) # Output: he11o wor1d

				
			
  1. `translate()`: Returns a copy of a string with specified characters replaced.
				
					string = "hello world"
translation_table = str.maketrans("l", "1")
new_string = string.translate(translation_table)
print(new_string) # Output: he11o wor1d

				
			
  1. `zfill()`: Pads a numeric string with zeros on the left until the specified width is reached.
				
					string = "42"
new_string = string.zfill(5)
print(new_string) # Output: 00042

				
			
  1. `expandtabs()`: Replaces tabs in a string with spaces.
				
					string = "hello\tworld"
new_string = string.expandtabs()
print(new_string) # Output: hello   world

				
			
  1. `encode()`: Encodes a string using a specified encoding.
				
					string = "hello world"
encoded_string = string.encode(encoding="utf-8")
print(encoded_string) # Output: b'hello world'

				
			

In the example above, we have a string containing the phrase “hello world”. We then use the encode() method to encode the string using the UTF-8 encoding. The resulting encoded string is a bytes object, which is denoted by the “b” prefix in the output.

  1. `format_map()`: Formats a string using a dictionary.
				
					person = {'name': 'Yash', 'age': 25}
print("My name is {name} and I am {age} years old.".format_map(person)) # Output: My name is Yash  and I am 25 years old.

				
			
  1. `isdecimal()`: Returns `True` if a string contains only decimal characters, otherwise `False`.
				
					num1 = "123"
num2 = "1.23"
print(num1.isdecimal()) # True
print(num2.isdecimal()) # False

				
			
  1. `isnumeric()`: Returns `True` if a string contains only numeric characters, otherwise `False`.
				
					num1 = "123"
print(num1.isnumeric()) # True

				
			
  1. `partition()`:It split the string into parts based on the first occurrence of the substring.
				
					string = "Sqatools is best for learning Python"
partitioned_string = string.partition("for")
print(partitioned_string) # Output: ( ‘Sqatools is best’, ‘for’, ‘learning Python’)

				
			

Leave a Comment