String methods in python with examples geeksforgeeks

EdithaElaine 52 Published: 07/22/2024

String methods in python with examples geeksforgeeks

I'm happy to respond in English for you!

Python's string methods are incredibly powerful and versatile, allowing developers to manipulate and transform strings in various ways. Here are some essential string methods in Python, along with examples:

lower(): This method converts all uppercase characters to lowercase.
>>> s = "HELLO WORLD"



s.lower()

'hello world'

upper(): As the name suggests, this method converts all lowercase characters to uppercase.
>>> s = "hello world"



s.upper()

'HELLO WORLD'

title(): This method capitalizes the first character of each word and makes all other characters lowercase.
>>> s = "hello world"



s.title()

'Hello World'

swapcase(): This method swaps the case of a string, i.e., converts uppercase to lowercase and vice versa.
>>> s = "HELLO WORLD"



s.swapcase()

'hELLO wORLD'

strip(): This method removes leading and trailing whitespace characters (spaces, tabs, newlines) from a string.
>>> s = "   hello world   "



s.strip()

'hello world'

lstrip() and rstrip(): These methods remove leading or trailing whitespace characters (respectively) from a string.
>>> s = "   hello world   "



s.lstrip() # Remove leading spaces

'helo world '

s.rstrip() # Remove trailing spaces

' hello world'

replace(): This method replaces all occurrences of a specified substring with another substring.
>>> s = "hello, world!"



s.replace("world", "universe")

'hellos, universe!'

split() and join(): These methods split or join strings using a specified separator.
>>> s = "hello,world,python"



s.split(",") # Split by commas

['hello', 'world', 'python']

"-".join(["hello", "world", "python"]) # Join with dashes

'hello-world-python'

format(): This method formats a string according to a specified format string.
>>> s = "{0} is {1}."



name = "John"

age = 30

s.format(name, age)

'John is 30.'

splitlines() and join(): These methods split or join strings by newline characters.
>>> s = "hellonworldnpython"



s.splitlines()

['hello', 'world', 'python']

"n".join(["hello", "world", "python"])

'hellonworldnpython'

These are just a few examples of the many string methods available in Python. Mastering these methods will help you become proficient in working with strings and performing text manipulation tasks.

String in Python

I'll respond in English and provide a string manipulation example using Python.

String manipulation is a crucial aspect of programming, as it allows you to process and transform text data according to your needs. In Python, strings are sequences of characters, often used to represent text, file paths, or other forms of text-like data.

Here's a simple example of how to manipulate strings in Python:

# Define a string

original_string = "Hello, my name is John and I am 30 years old."

Split the string into individual words

words = original_string.split()

Convert the first word (hello) to uppercase

first_word_uppercase = words[0].upper()

print("The original string is:", original_string)

print("First word in uppercase is:", first_word_uppercase)

output:

The original string is: Hello, my name is John and I am 30 years old.

First word in uppercase is: HELLO

Extract the last three words

last_three_words = " ".join(words[-3:])

print("Last three words are:", last_three_words)

In this example:

We define a string with some sample text. The split() method is used to split the string into individual words, which are stored in the words list. We convert the first word (Hello) to uppercase using the upper() method and store it in the first_word_uppercase variable. We print the original string and the modified first word. To extract the last three words from the original string, we use negative indexing (words[-3:]) and join them with spaces using the join() function. This is done to maintain readability of the output string.

In the Python code above:

The split() method splits the input string into a list of individual words based on whitespace characters (spaces). We store these individual words in the words list. We use indexing to access and modify specific parts of the strings. In this example, we convert the first word ("Hello") to uppercase by accessing its first element (words[0]) and calling the upper() method on it. The last three words are accessed using negative indexing (words[-3:]). Negative indices count from the end of the list; in our case, -1 would refer to the last element, -2 the second-to-last, and so forth.

String manipulation is crucial in various applications such as data processing, file handling, and natural language processing. This example illustrates some fundamental operations and techniques for working with strings in Python, allowing you to build on your understanding of programming concepts and expand your skillset.