Python Strings

Strings are the basic data types in Python used to represent sequences of characters. In Python, strings are immutable, which means that they cannot be changed after they are created. This makes it possible to perform operations that return a new string without modifying the original string.

Creating String

You can create a string that contains characters by enclosing them in either single quotes (”) or double quotes (” “). For example:

message = 'Hello, World!'
name = "Python Gdb"

You can create strings on multiple lines by using triple quotes (”’ ”’) or (“”” “””). This is useful when you want to span a string over multiple lines:

multiline_string = '''This is a
multiline
string.'''

String Concatenation

You can concatenate strings using the “+” operator. It is the concatenation of the two operands that creates the new string.

greeting = "Hello"
name = "Python"
message = greeting + ", " + name + "!"

String Formatting

Python provides various methods for formatting strings. A common way is to use the format() method:

name = "Alice"
age = 25
message = "My name is {}, and I am {} years old.".format(name, age)

In Python 3.6 and above, you can also use f-strings, which provide a concise way to encapsulate expressions in string literals:

name = "Alice"
age = 25
message = f"My name is {name}, and I am {age} years old."

String Indexing and Slicing

You can access individual characters of a string using indexing. In Python, indexing starts at 0.

For example:

text = "Hello, World!"
print(text[0])  # Output: 'H'

You can extract a part of a string using slicing:

text = "Hello, World!"
print(text[7:12])  # Output: 'World'

String Method

There are various string methods available in Python for converting and parsing strings. Some common methods include lower(), upper(), strip(), split(), join(), and replace().

Here are some examples:

text = "   Hello, World!   "
print(text.lower())  # Output: '   hello, world!   '
print(text.strip())  # Output: 'Hello, World!'
print(text.split(','))  # Output: ['   Hello', ' World!   ']
print("-".join(["apple", "banana", "cherry"]))  # Output: 'apple-banana-cherry'
print(text.replace("Hello", "Hi"))  # Output: '   Hi, World!   '

These are just some of the basic operations and methods available for working with strings in Python. The Python string module provides many more functions and methods for string manipulation and formatting.

Example Combination of String

# Creating strings
message = 'Hello, World!'
name = "John Doe"

# Multiline string
multiline_string = '''This is a
multiline
string.'''

# String concatenation
greeting = "Hello"
message = greeting + ", " + name + "!"

# String formatting
age = 25
message = "My name is {}, and I am {} years old.".format(name, age)

# String indexing and slicing
text = "Hello, World!"
first_char = text[0]
substring = text[7:12]

# String methods
text = "   Hello, World!   "
lowercase_text = text.lower()
stripped_text = text.strip()
split_text = text.split(',')
joined_text = "-".join(["apple", "banana", "cherry"])
replaced_text = text.replace("Hello", "Hi")

# Print outputs
print(message)
print(multiline_string)
print(greeting)
print(first_char)
print(substring)
print(lowercase_text)
print(stripped_text)
print(split_text)
print(joined_text)
print(replaced_text)

Output

My name is John Doe, and I am 25 years old.
This is a
multiline
string.
Hello
H
hello, world!
Hello, World!
['   Hello', ' World!   ']
apple-banana-cherry
   Hi, World!