1/5
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
String concatenation
Is the process of combining two or more strings into a single string.
str1 = "Hello"
str2 = "World"
concatenatedstr = str1 + str2 print("Concatenated string:", concatenatedstr)
# Using f-string (formatted string literals)
name = "John"
greeting = f"Hello, {name}!"
print(greeting)
String comparison
Is used to determine the order or equality of two strings. In Python, you can compare strings using comparison operators such as ==, !=,
str1 = "banana"
str2 = "banana"
result = str1 == str2
print(result) # Output will be True
String copying
Involves copying the contents of one string to another. In Python, since strings are immutable, you can create a copy of a string simply by assigning it to a new variable.
str1 = "Hello"
str2 = str1
print("Copied string:", str2)
Implicit typecasting
Also known as automatic type conversion, occurs when Python automatically converts a value from one data type to another without explicit instructions from the programmer. This conversion is based on the rules defined by the language. Implicit typecasting is generally performed when a narrower data type is assigned to a broader data type or when an operation involves different data types.
x = 10
y = 3.14
result = x + y # Implicit typecasting from int to float
print(result) # Output: 13.14
Explicit typecasting
Also known as type conversion, requires the programmer to explicitly specify the desired data type. It involves using built-in functions to inform Python about the intended conversion.
int(): Converts a value to an integer data type.
float(): Converts a value to a floating-point data type.
str(): Converts a value to a string data type.
x = 65
y = chr(x) # Explicit typecasting from int to char (using the chr() function)
print(y) # Output: 'A'
Character
x = 65
y = chr(x)