01string in Pythonpdf
01string in Pythonpdf
BS COMPUTER SCIENCE
quest university
javedoad85@gmail.com
• a='javed'
• b='oad'
• print(a+b*2) #javedoadoad
Example
• # Example of built-in
functions
• print("Hello, World!")
• result = len([1, 2, 3])
Concatenation
In Python, • first_name = "John"
concatenation refers • last_name = "Doe"
to the operation • full_name = first_name
of joining two or more + " " + last_name
strings • print(full_name)
together into one
continuous string. This
is done using
the + operator.
Concatenation
Important Points: • age = 30
•Only strings can be • message = "I am " +
concatenated: If you try str(age) + " years
to concatenate a string old."
•with a number (int or
float), Python will
• raise a TypeError. You
must convert the number to
a string first using str().
Concatenation
• Str[0] is h
• Str[1] is e
sequence[start:stop:step]
Example with a String:
text = "Python"
num = 100
string_num = str(num) # Output: "100"
lower(): Converts all characters in the string to lowercase.
text = "Hello"
print(text.lower()) # Output: "hello"
upper(): Converts all characters in the string to uppercase.
text = "Hello"
print(text.upper()) # Output: "HELLO"
replace(old, new): Replaces occurrences of a substring within the string with a new substring
text = "Hello World"
print(text.replace("World", "Python")) #
Output: "Hello Python"
find(substring): Returns the index of the first occurrence of a substring within the string. Returns -1 if the substring is not found.
text = "Hello"
index = text.find("e") # Output: 1
split(separator): Splits the string into a list of substrings based on a separator.
text = "apple,banana,cherry"
fruits = text.split(",") # Output: ['apple',
'banana', 'cherry']
join(iterable): Joins elements of an iterable (e.g., list) into a single string, with a specified separator.
fruits = ['apple', 'banana', 'cherry']
result = ", ".join(fruits) # Output: "apple,
banana, cherry"
text = " Hello "
print(text.strip()) # Output: "Hello"
startswith(prefix): Returns True if the string starts with the specified prefix, otherwise False.
text = "Hello"
result = text.startswith("He") # Output: True
endswith(suffix): Returns True if the string ends with the specified suffix, otherwise False .
text = "Hello"
result = text.endswith("lo") # Output: True
text = "Python Programming"
# Converting to uppercase
print(text.upper()) # Output: "PYTHON PROGRAMMING"
# Replacing a substring
print(text.replace("Python", "Java")) # Output: "Java Programming"
# Finding a substring
print(text.find("Pro")) # Output: 7