Back to Browse

String Methods in Python Part 1 | Python String Methods | Python String Built-in Functions

163 views
Premiered Aug 18, 2023
16:10

In Python, a string is a sequence of characters enclosed within either single ('') or double ("") quotation marks. Strings are a fundamental data type and are used to represent textual data. They can contain letters, numbers, symbols, and even spaces. Here's a brief explanation of some key features and operations related to strings in Python: Creating Strings: You can create strings using single or double quotes. For example: single_quoted = 'Hello, devvrat!' double_quoted = "Knowledgevilla is great channel" String Concatenation: You can combine strings using the + operator. For example: greeting = "Hello" name = "Devvrat" full_greeting = greeting + ", " + name String Indexing: Each character in a string has a unique index, starting from 0 for the first character. You can access individual characters using indexing. For example: message = "Python" first_char = message[0] # 'P' second_char = message[1] # 'y' String Slicing: You can extract a portion of a string using slicing. Slicing is done using the format [start:end], where start is the index of the first character you want and end is the index of the character just after the last character you want. For example: phrase = "Python Programming" substring = phrase[7:15] # "Programming" String Methods: Python provides various built-in methods for working with strings. Some common methods include: len(): Returns the length of the string. lower(): Converts the string to lowercase. upper(): Converts the string to uppercase. strip(): Removes leading and trailing whitespace. split(): Splits the string into a list of substrings based on a delimiter. replace(): Replaces a specified substring with another substring. And many more... String Formatting: You can format strings using various techniques, such as: F-strings (formatted string literals) str.format() method % operator String Escape Characters: Escape characters are used to represent characters that can't be directly inserted into a string. For example, \n represents a newline character, and \" represents a double quote within a double-quoted string. Raw Strings: Raw strings are used to ignore escape characters. They are defined by prefixing the string with an 'r' or 'R'. For example: r"C:\path\to\file" Strings are immutable in Python, which means you can't modify a string in place. Instead, you create new strings when performing operations like concatenation or replacement. Here are a few examples using the string "devvrat knowledgevilla": String Length: string = "devvrat knowledgevilla" length = len(string) # 22 Uppercase and Lowercase: string = "devvrat knowledgevilla" uppercase = string.upper() # "DEVVRAT KNOWLEDGEVILLA" lowercase = string.lower() # "devvrat knowledgevilla" String Splitting: string = "devvrat knowledgevilla" words = string.split() # ['devvrat', 'knowledgevilla'] String Indexing: string = "devvrat knowledgevilla" first_char = string[0] # 'd' third_char = string[2] # 'v' String Slicing: string = "devvrat knowledgevilla" substring = string[8:19] # "knowledgevilla" String Concatenation: greeting = "Hello" name = "Devvrat" message = greeting + ", " + name # "Hello, Devvrat" String Replacement: string = "devvrat knowledgevilla" new_string = string.replace("knowledge", "information") # "devvrat informationvilla" Finding Substring: string = "devvrat knowledgevilla" index = string.find("knowledge") # 8 String Formatting (F-strings): name = "Devvrat" age = 25 formatted_string = f"My name is {name} and I am {age} years old." # "My name is Devvrat and I am 25 years old." String with Escape Characters: message = "This is a \"quote\" within a string." #str.capitalize() name = "devvrat knowledgevilla" capitalized_name = name.capitalize() print(capitalized_name) #str.casefold() name = "Devvrat Knowledgevilla" casefolded_name = name.casefold() print(casefolded_name) #str.center(width[, fillchar]) name = "devvrat knowledgevilla" centered_name = name.center(50, '*') print(centered_name) #str.count(sub[, start[, end]]) name = "devvrat knowledgevilla" substring = "knowledge" count = name.count(substring) print(f"The substring '{substring}' appears {count} times in the string.") #str.encode(encoding='utf-16', errors='strict') name = "devvrat knowledgevilla" encoded_name = name.encode(encoding='utf-16') print(encoded_name)

Download

0 formats

No download links available.

String Methods in Python Part 1 | Python String Methods | Python String Built-in Functions | NatokHD