In Python programming, a string is a sequence of characters enclosed within either single quotes ''
or double quotes ””
. Strings are one of the fundamental data types in Python and are used extensively in programming to represent text-based data. In this article, we will discuss the properties of strings in Python, their importance, and some of the most commonly used methods for working with strings.
Let see some string properties and examples for each one:
- Strings are immutable: That means we can not assign a new value to the string- Strings are immutable: That means we can not assign a new value to the string
my_string = "Hello, medium!"
my_string[0] = "J" #assigning new value my_string[0]'
print(my_string) #output => TypeError: 'str' object does not support item assignment
2. Strings can be enclosed in single or double quotes:
my_string = "Hello, medium!"
second_string = 'Hello, medium!'
print(my_string) #output => Hello, medium!
print(second_string) #output => Hello, medium!
3. Strings are indexed and sliced:
my_string = "Hello, medium!"
#getting a especific character with the index
print(my_string[0]) #output => H
print(my_string[1]) #output => e
print(my_string[2]) #output => l
print(my_string[3]) #output => l
print(my_string[3]) #output => o
# slicing the string to get a substring
print(my_string[0:6]) #output => Hello,
print(my_string[0:-1]) #output => Hello, medium
print(my_string[7:14]) #output => medium!
4. Strings can be concatenated: You can concatenate two or more strings to create a new string. This is often done using the +
operator.
fist_string = "Hello, "
second_string = "medium!"
my_string = fist_string + second_string
print(my_string) #output => Hello, medium!
5. Strings can be formatted using placeholders and formatting specifiers to insert variables, values, and expressions into a string. Here are some examples of how to format strings in Python:
# Using placeholders
name = "Medium"
month = "August"
year = 2012
my_strig = "Hello, I am {} and I was lanched in {} {}".format(name,month, year)
print(my_strig) # Output=> Hello, I am Medium and I was lanched in August 2012
# Using formatting specifiers
pi = 3.14159265359
formatted_pi = "The value of pi is approximately {:.2f}".format(pi)
print(formatted_pi) # Output=> The value of pi is approximately 3.14
# Using f-strings
name = "Bob"
age = 30
greeting = f"Hello, my name is {name} and I am {age} years old"
print(greeting) # Output=> Hello, my name is Bob and I am 30 years old
6. Strings can be compared:
my_string1 = "Hello, medium!"
my_string2 = "hello, medium!"
print(my_string1 == my_string2) #output => False
print(my_string1.lower() == my_string2.lower()) #ouutput => True
Python provides several built-in methods for working with strings. Here are some of the most commonly used methods:
len()
: This method returns the length of a string (the number of characters it contains). For example:
my_string1 = "Hello, medium!"
print(len(my_string1)) #output => 14
upper()
: This method returns a new string with all characters converted to uppercase. For example:
my_string = "Hello, medium!"
print(my_string.upper()) #Output => HELLO, MEDIUM!
lower()
: This method returns a new string with all characters converted to lowercase. For example:
my_string = "Hello, medium!"
print(my_string.lower()) #Output => hello, medium!
strip()
: This method returns a new string with any leading or trailing whitespace removed. For example:
my_string = " Hello, medium! "
print(my_string.strip()) #Output => 'Hello, medium!'
split()
: This method splits a string into a list of substrings based on a specified delimiter. For example:
my_string = "Hello, medium!"
print(my_string.split(',')) #Output => ['Hello', ' medium!']
join()
: This method concatenates a list of strings into a single string using a specified delimiter. For example:
my_list = ['Hello', 'medium!']
delimiter = ', '
my_string = delimiter.join(my_list)
print(my_string) #Output => 'Hello, medium!'
In short strings in Python are a type of data that are really useful for working with text-based information. They have a lot of different properties that allow you to do things like find specific characters or words, combine different strings together, and format them in different ways. Because of all these properties, strings are used a lot in programming whenever you need to work with text data.
Thanks for reading I hope this article helps you! 😊