Common String Methods
123456sample_text = "hello world" print(sample_text.lower()) # Output: hello world print(sample_text.upper()) # Output: HELLO WORLD print(sample_text.capitalize()) # Output: Hello world print(sample_text.title()) # Output: Hello World
Case conversion is essential when you need to standardize text for comparison, searching, or display. The .lower() and .upper() methods convert all characters in a string to lowercase or uppercase, which is useful for case-insensitive comparisons. The .capitalize() method makes only the first character uppercase and the rest lowercase, which is helpful for formatting names or sentences. The .title() method capitalizes the first letter of each word, making it ideal for titles or headings.
12345678910111213text = "Data cleaning is important. Data is everywhere." # Find the position of the first occurrence of "Data" position = text.find("Data") print(position) # Output: 0 # Replace "Data" with "Information" cleaned_text = text.replace("Data", "Information") print(cleaned_text) # Output: Information cleaning is important. Information is everywhere. # Count how many times "is" appears count_is = text.count("is") print(count_is) # Output: 2
The .replace() method is especially useful for simple data cleaning tasks, such as removing unwanted characters, fixing misspellings, or standardizing terms throughout a dataset. By replacing specific substrings, you can quickly prepare text for further analysis or presentation.
1. What does the .find() method return if the substring is not found?
2. Which methods can be used to change the case of a string?
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Awesome!
Completion rate improved to 6.67
Common String Methods
Scorri per mostrare il menu
123456sample_text = "hello world" print(sample_text.lower()) # Output: hello world print(sample_text.upper()) # Output: HELLO WORLD print(sample_text.capitalize()) # Output: Hello world print(sample_text.title()) # Output: Hello World
Case conversion is essential when you need to standardize text for comparison, searching, or display. The .lower() and .upper() methods convert all characters in a string to lowercase or uppercase, which is useful for case-insensitive comparisons. The .capitalize() method makes only the first character uppercase and the rest lowercase, which is helpful for formatting names or sentences. The .title() method capitalizes the first letter of each word, making it ideal for titles or headings.
12345678910111213text = "Data cleaning is important. Data is everywhere." # Find the position of the first occurrence of "Data" position = text.find("Data") print(position) # Output: 0 # Replace "Data" with "Information" cleaned_text = text.replace("Data", "Information") print(cleaned_text) # Output: Information cleaning is important. Information is everywhere. # Count how many times "is" appears count_is = text.count("is") print(count_is) # Output: 2
The .replace() method is especially useful for simple data cleaning tasks, such as removing unwanted characters, fixing misspellings, or standardizing terms throughout a dataset. By replacing specific substrings, you can quickly prepare text for further analysis or presentation.
1. What does the .find() method return if the substring is not found?
2. Which methods can be used to change the case of a string?
Grazie per i tuoi commenti!