String Methods in Python Part 3 | Python String Methods | Python String Built-in Functions
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: str.endswith(): Checks if a string ends with a specified substring. str.expandtabs(): Replaces tab characters with spaces based on specified tab size. str.find(): Searches for a substring within a string and returns the index of its first occurrence, or -1 if not found. str.format(): Formats a string by replacing placeholders with values from provided arguments or keyword arguments. str.format(): Same as above, it's likely included twice in your list by mistake. And many more... #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) #str.endswith(suffix[, start[, end]]) text = "Hello, world!" result1 = text.endswith("world!") result2 = text.endswith("world") result3 = text.endswith("World!") print(f"Does the string end with 'world!': {result1}") print(f"Does the string end with 'world': {result2}") print(f"Does the string end with 'World!': {result3}") #str.expandtabs(tabsize=8) # Example string with tab characters text = "Hello\tworld!\tPython\tis\tawesome." expanded_text = text.expandtabs(16) print("Original string:") print(text) print("\nExpanded string with tab size of 4:") print(expanded_text) #str.find(sub[, start[, end]]) text = "Hello, this is an example string." index1 = text.find("is") index2 = text.find("is", 10) index3 = text.find("is", 10, 20) print(f"Index of 'is': {index1}") print(f"Index of 'is' starting from index 10: {index2}") print(f"Index of 'is' between index 10 and 20: {index3}") #str.format(*args, **kwargs) # Using positional arguments name = "Alice" age = 30 message = "My name is {} and I am {} years old.".format(name, age) print(message) # Using keyword arguments description = "The {item} costs ₹{price}.".format(item="book", price=19.99) print(description) # Using both positional and keyword arguments feedback = "Player {} achieved a score of {score}.".format("Virat", score=85) print(feedback) # Using index-based placeholders fruit1 = "apples" fruit2 = "bananas" fruit_sentence = "I like {1} and {0}.".format(fruit1, fruit2) print(fruit_sentence) # Using named placeholders student = {'name': 'Harvi', 'age': 22} student_info = "Student's name: {name}, age: {age}".format(**student) print(student_info) #str.format_map(mapping) # Define a dictionary with placeholder values data = { 'name': 'John', 'age': 30, 'country': 'USA' } template = "My name is {name}, I am {age} years old, and I am from {country}." # Format the string using the map formatted_string = template.format_map(data) # Print the formatted string print(formatted_string) #str.index(sub[, start[, end]]) # Define a string text = "Hello, world! Welcome to Python." # Find the index of a substring index = text.index("world") # Print the index print("Index of 'world':", index)
Download
0 formatsNo download links available.