Python offers various ways to work with strings, providing flexibility and versatility in handling textual data. Here's a detailed explanation of some of the common string types and operations in Python:
1. Single Quotes (' ') and Double Quotes (" "):
- In Python, you can create strings using either single quotes or double quotes
Input is Shown Below :
#Here is some strings
#Code by IAH (infinity Aggarwal Harshul)
single_quoted = 'This is a sibgle quoted string.' #here we take a strings and storing it in single_quoted variable
double_quoted = "This is a double quoted string." #here we take a strings and storing it in double_quoted variable
print (single_quoted) #print function will directly print the string storing inside the variables
print(double_quoted)
#The output will show the print of some strings stored inside them
Output is Shown Below :
2. Triple Quotes (''' ''' or """ """):
- Triple quotes allow multiline strings in Python.
- Can be enclosed with either single or double quotes.
- Useful for docstrings (documentation strings) and multiline strings.
Input is Shown Below :
#Here is multiline strings
#Code by IAH (infinity Aggarwal Harshul)
multiline_string = '''
This is a multiline string #here we gonna take a multiline string
It can span across multiple lines.
'''
print(multiline_string) #print the input stored inside the multiline_string
#The output will show the print of some strings stored inside them
Output is Shown Below :
3. Raw Strings (r' ' or r" "):
- Raw strings treat backslashes as literal characters.
- Useful for regular expressions or file paths.
Input is Shown Below :
Code :
#Here is raw strings
#Code by IAH (infinity Aggarwal Harshul)
raw_string = r'C:\Users\Username\Documents' #Lets take a file location and stored inside a raw string
print(raw_string) #print the input stored inside the raw_string
#The output will show the print the file location stored inside raw string
Output is Shown Below :
4. Formatted Strings (f' ' or f" "):
- Introduced in Python 3.6, formatted strings allow embedding expressions inside strings.
- Expression inside curly braces
{} is evaluated and replaced with its value.
Input is Shown Below :
Code :
#Here is formatted strings
#Code by IAH (infinity Aggarwal Harshul)
name = 'Alice' #Lets take any random string and store it in name variable
age = 30 #Lets take any random value and store it in age variable
formatted_string = f'Name: {name}, Age: {age}' #Lets take some information inside the formatted string
print(formatted_string) #print the string ehich storing some information inside it
#The output will show the print the information stored inside formatted string
Output is Shown Below :
5. Unicode Strings:
- Python 3 strings are Unicode by default.
- Supports a wide range of characters including emojis, accented characters, etc.
Input is Shown Below :
Code :
#Here is unciorn string
#Code by IAH (Infinity Aggarwal Harshul)
unicorn_string = 'Hello, 😊!' #lets take some information inside the unicorn string
print(unicorn_string) #print the unicorn string
#The Output will show the print of the information stored inside the unicorn string
Output is Shown Below :
6. Byte Strings (b' ' or b" "):
- Represented as sequences of bytes rather than Unicode characters.
- Prefixing a string with
b makes it a byte string. - Used for handling binary data.
Input is Shown Below :
Code :
#Here is byte string
#Code by IAH (Infinity Aggarwal Harshul)
byte_string = b'Hello, world!' #lets take some information inside the byte string
print(byte_string) #print the byte string
#The Output will show the print of the information stored inside the byte string
Output is Shown Below :
7. String Operations:
Input is Shown Below :
Code :
#Here is combined string
#Code by IAH (Infinity Aggarwal Harshul)
combined_string = 'Hello'+' '+'World' #lets take some information inside the combined string
print(combined_string) #print the combined string
#The Output will show the print of the information stored inside the combined string
Output is Shown Below :
- Repetition: Strings can be repeated using the
* operator.
Input is Shown Below :
Code :
#Here is repeated string
#Code by IAH (Infinity Aggarwal Harshul)
repeated_string = 'Hello' * 3 #lets take some information inside the repeated string
print(repeated_string) #print the repeated string
#The Output will show the print of the information stored inside the repeated string
Output is Shown Below :
- Indexing and Slicing: Access individual characters or substrings using indexing and slicing.
Input is Shown Below :
Code :
#Here is my string
#Code by IAH (Infinity Aggarwal Harshul)
my_string = 'Python' #Lets take some information inside the mystring
print(my_string[0]) #Output: 'P'
print(my_string[2:5]) #Output: 'tho'
print(my_string[2:]) #Output: 'thon'
#The Output will show the print of the information stored inside the mystring
Output is Shown Below :
- String Methods: Python provides numerous built-in string methods for various operations like manipulation, searching, formatting, etc.
Input is Shown Below :
Code :
#Here is my string
#Code by IAH (Infinity Aggarwal Harshul)
my_string = 'Hello, World!' #Lets take some information inside the mystring
print(my_string.upper()) #Output: 'Hello, WORLD!'
print(my_string.find('World')) #Output: 7(it means word is at index 7 according to words count )
#The Output will show the print of the information stored inside the mystring
Output is Shown Below :
These are some of the fundamental string types and operations available in Python, offering great flexibility and ease in handling textual data.
Comments
Post a Comment