TYPES OF STRINGS

1.SLICING STRING

String slicing allows you to extract a portion of a string by specifying the start and end indices. The start index is inclusive, meaning that the character at that index is included in the slice, while the end index is exclusive, meaning that the character at that index is not included in the slice.

Here’s an example of string slicing in Python:

message = "Hello, World!"
substring = message[7:12]
print(substring)

Output:
World

In this example, we have a string message which contains the phrase “Hello, World!”. By using slicing, we extract a portion of the string starting from index 7 (inclusive) and ending at index 12 (exclusive). The resulting substring is assigned to the variable substring and then printed, which outputs “World”.

You can also omit the start or end index to slice from the beginning or until the end of the string, respectively. Here are a few examples:

message = "Hello, World!"
substring1 = message[:5]    # Slice from the beginning until index 5 (exclusive)
substring2 = message[7:]    # Slice from index 7 (inclusive) until the end
substring3 = message[:]     # Slice the entire string

print(substring1)
print(substring2)
print(substring3)

Output:


Hello
World!
Hello, World!


In the first example, we slice the string from the beginning until index 5 (exclusive), resulting in "Hello". In the second example, we slice the string from index 7 (inclusive) until the end, resulting in "World!". Lastly, we slice the entire string by omitting both the start and end indices, resulting in the original string "Hello, World!".

String slicing is a powerful tool for extracting substrings from strings and is commonly used in tasks such as parsing, text manipulation, and data extraction.

2.CONCATENATION OF STRING:
To concatenate, or combine, two strings you can use the + operator.For concatenation of string we use "+" symbol

Example 1:

Merge variable a with variable b into variable c:

a = "Hello"
b = "World"
c = a + b
print(c)

OUTPUT:

HelloWorld

Example 2:

To add a space between them, add a " ":

a = "Hello"
b = "World"
c = a + " " + b
print(c)

OUTPUT:

Hello World
Website | + posts

Post a comment