STRINGS

STRINGS

STRINGS

STRINGS:

In Python, a string is a sequence  of characters enclosed within single, double, or triple quotes. Strings are a fundamental data type in Python, and they are used extensively for storing and manipulating textual data.

Here are some basic operations that can be performed on strings in Python:

  1. Concatenation: To join two or more strings together, you can use the + For example, “Hello ” + “world!” will give you the string “Hello world!”.
  2. Indexing and Slicing: You can access individual characters of a string by indexing them with square brackets. For example, “hello”[0] will give you the character ‘h’. You can also extract a slice of a string by specifying a range of indices. For example, “hello”[1:3] will give you the string “el”.
  3. Length: You can find the length of a string using the len() For example, len(“hello”) will give you the integer 5.
  4. String Methods: Python provides a variety of built-in methods that can be used to manipulate strings. For example, you can use the upper() method to convert a string to uppercase, or the replace() method to replace one substring with another.

Here are some examples:

				
					# Concatenation
greeting = "Hello "
name = "Alice"
message = greeting + name
print(message)                                        # Output: "Hello Alice"
	
# Indexing and Slicing
my_string = "Python is cool"
print(my_string[0])                                 # Output: "P"
print(my_string[7:9])                             # Output: "is"
print(my_string[-4:])                              # Output: "cool"

# Length
my_string = "Hello World!"
print(len(my_string))                             # Output: 12

# String Methods
my_string = "Hello World!"
print(my_string.upper())                                 # Output: "HELLO WORLD!"
print(my_string.replace("Hello", "Hi"))       # Output: "Hi World!"
				
			
Join To Get Our Newsletter
Spread the love