String :
In Python, a string is a sequence of characters enclosed in quotation marks (either single or double).
For example:
my_string = "Hello, World!"
There are many built-in functions and methods for manipulating strings in Python. Some common ones include:
len(string): returns the length of a string.
string.upper(): returns a copy of the string with all characters in uppercase.
string.lower(): returns a copy of the string with all characters in lowercase.
string.strip(): returns a copy of the string with leading and trailing whitespace removed.
string.replace(old, new): returns a copy of the string with all occurrences of the specified old string replaced by the specified new string.
string.split(separator): returns a list of substrings in the string, delimited by the specified separator string.
string in substring: returns True if the specified substring is found within the string, False otherwise.
You can also use the + operator to concatenate strings and the * operator to repeat strings.
str1 = "Hello"
str2 = " World!"
greeting = str1 + str2
print(greeting) # "Hello World!"
repeated = str1 * 3
print(repeated) # "HelloHelloHello"
You can also use string formatting to embed variables into a string, using curly braces {} as placeholders.
name = "John"
age = 20
print("My name is {} and I am {} years old.".format(name, age)) # My name is John and I am 20 years old.
You can also use f-strings, which is a new way to embed variables into a string in python 3.6 and above
name = "John"
age = 20
print(f"My name is {name} and I am {age} years old.") # My name is John and I am 20 years old.




0 Comments