0% found this document useful (0 votes)
7 views4 pages

Python Threading and String Methods

Uploaded by

sabarin2026
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Python Threading and String Methods

Uploaded by

sabarin2026
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import threading

import time

def print_numbers():
for i in range(5):
[Link](1)
print(i)

# Create threads
thread1 = [Link](target=print_numbers)

# Start threads
[Link]()

# Wait for threads to finish


[Link]()

[Link]

import threading
import time

event = [Link]()

def waiter():
print("Waiting for event...")
[Link]()
print("Event received!")

def setter():
[Link](2)
[Link]()
print("Event set!")

t1 = [Link](target=waiter)
t2 = [Link](target=setter)

[Link]()
[Link]()

[Link]()
[Link]()

abstract

from abc import ABC, abstractmethod

class Animal:
@abstractmethod
def get(self):
pass
class Dog(Animal):
def get(self):
return "Bark"

dog = Dog()

print([Link]()) # Output: Bark

String methods

[Link]()

Description: Converts all characters in the string to uppercase.


Example: 'hello'.upper() → 'HELLO'

[Link]()

Description: Converts all characters in the string to lowercase.


Example: 'HELLO'.lower() → 'hello'

[Link]()

Description: Converts the first character of each word to uppercase and the rest to
lowercase.
Example: 'hello world'.title() → 'Hello World'

[Link]()

Description: Converts the first character to uppercase and the rest to lowercase.
Example: 'hello world'.capitalize() → 'Hello world'

[Link]([chars])

Description: Removes leading and trailing whitespace or specified characters.


Example: ' hello '.strip() → 'hello'
Example: '---hello---'.strip('-') → 'hello'

[Link]([chars])

Description: Removes leading whitespace or specified characters.


Example: ' hello'.lstrip() → 'hello'
Example: '---hello'.lstrip('-') → 'hello'

[Link]([chars])

Description: Removes trailing whitespace or specified characters.


Example: 'hello '.rstrip() → 'hello'
Example: 'hello---'.rstrip('-') → 'hello'

[Link](old, new[, count])

Description: Replaces occurrences of a substring with another substring. The count


parameter specifies the number of replacements.
Example: 'hello world'.replace('world', 'Python') → 'hello Python'
Example: 'hello hello'.replace('hello', 'hi', 1) → 'hi hello'
[Link](sub[, start[, end]])

Description: Returns the lowest index where the substring sub is found. Returns -1
if the substring is not found.
Example: 'hello world'.find('world') → 6
Example: 'hello world'.find('world', 0, 5) → -1

[Link](sub[, start[, end]])

Description: Returns the highest index where the substring sub is found. Returns -1
if the substring is not found.
Example: 'hello world world'.rfind('world') → 12

[Link]([sep[, maxsplit]])

Description: Splits the string into a list of substrings based on the delimiter
sep. maxsplit defines the maximum number of splits.
Example: 'hello world'.split() → ['hello', 'world']
Example: 'hello-world-python'.split('-', 1) → ['hello', 'world-python']

[Link](iterable)

Description: Joins elements of an iterable (e.g., a list) into a single string,


separated by the string str.
Example: ', '.join(['apple', 'banana', 'cherry']) → 'apple, banana, cherry'

[Link](*args, **kwargs)

Description: Formats strings using placeholders {} and provides a way to inject


variables into the string.
Example: 'Hello, {}!'.format('Alice') → 'Hello, Alice!'
Example: 'The value is {:.2f}'.format(3.14159) → 'The value is 3.14'

[Link](width)

Description: Pads the string with zeros on the left to ensure it reaches the
specified width.
Example: '42'.zfill(5) → '00042'

[Link]()

Description: Checks if all characters in the string are digits.


Example: '12345'.isdigit() → True
Example: '12a45'.isdigit() → False

[Link]()

Description: Checks if all characters in the string are alphabetic.


Example: 'hello'.isalpha() → True
Example: 'hello1'.isalpha() → False

[Link]()

Description: Checks if all characters in the string are whitespace.


Example: ' '.isspace() → True
Example: ' a '.isspace() → False

[Link]()
Description: Checks if the string is in title case (each word starts with an
uppercase letter).
Example: 'Hello World'.istitle() → True
Example: 'hello world'.istitle() → False

[Link]()

Description: Checks if all characters in the string are lowercase.


Example: 'hello'.islower() → True
Example: 'Hello'.islower() → False

[Link]()

Description: Checks if all characters in the string are uppercase.


Example: 'HELLO'.isupper() → True
Example: 'Hello'.isupper() → False

You might also like