String methods in python with examples w3schools

Sam 23 Published: 08/19/2024

String methods in python with examples w3schools

I'd be happy to help! Here's a detailed explanation of string methods in Python using w3schools as a reference:

String Methods

Python strings support many built-in methods that can perform various operations on the string. These methods are very powerful and allow you to manipulate your strings in many different ways.

Here is an example of some of these methods:

my_string = "Hello, World!"
upper() method: Converts a string into uppercase.

print(my_string.upper()) # Output: HELLO, WORLD!

lower() method: Converts a string into lowercase.

print(my_string.lower()) # Output: hello, world!

swapcase() method: Swaps the case of the string.

print(my_string.swapcase()) # Output: hELLO, wORLD!

title() method: Convert a string to title case (first letter of each word is capitalized).

print(my_string.title()) # Output: Hello, World!

capitalize() method: Capitalize first character and make all other characters lowercase.

print(my_string.capitalize()) # Output: Hello, world!

strip() method: Remove leading and trailing characters from the string.

print(my_string.strip("h")) # Output: ello, World!

lstrip() method: Remove leading characters from the string.

print(my_string.lstrip("h")) # Output: ello, World!

rstrip() method: Remove trailing characters from the string.

print(my_string.rstrip("d! ")) # Output: Hello, Worl

count() method: Count the number of occurrences of a substring in the string.

print(my_string.count("o")) # Output: 2

find() method: Find the index of the first occurrence of a substring in the string.

print(my_string.find("o")) # Output: 4

rfind() method: Find the index of the last occurrence of a substring in the string.

print(my_string.rfind("o")) # Output: 7

replace() method: Replace a specified phrase with another specified phrase.

print(my_string.replace("World", "Python")) # Output: Hello, Python!

Formatting Strings

You can use various formatting methods to control the display of your strings. Here are some examples:

age = 25

name = "John"

format() method: Format a string into another string.

print("{0} is {1} years old".format(name, age)) # Output: John is 25 years old

% operator: Format a string using the % operator.

print("%s is %d years old" % (name, age)) # Output: John is 25 years old

String Formatting

You can also format strings to include various types of data. Here are some examples:

pi = 3.14159

e = 2.71828

format() method: Format a string into another string.

print("Pi is {:.2f} and Euler's number is {:.2f}".format(pi, e)) # Output: Pi is 3.14 and Euler's number is 2.72

Splitting Strings

You can split strings using various methods. Here are some examples:

my_string = "Hello,World!Goodbye,Cruel World!"
split() method: Split a string into a list where each word is a list item.

print(my_string.split(",")) # Output: ['Hello', 'World!", "Goodbye', "Cruel World!']

rsplit() method: Split the string from right side and return a list.

print(my_string.rsplit("!")) # Output: ["Hello,World;Goodbye,Cruel World"]

These are just some of the many powerful string methods that Python provides. With these methods, you can perform various operations on your strings to manipulate them in different ways.

I hope this helps! Let me know if you have any questions.

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.