-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString Operations.py
More file actions
36 lines (29 loc) · 1.14 KB
/
String Operations.py
File metadata and controls
36 lines (29 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# String Concatenation (joining strings together)
first_name = "X"
last_name = "L117"
full_name = first_name + "-" + last_name
print("Full name:", full_name) # Output: X-L117
# String Repetition
echo = "Hello! " * 3
print("Echo:", echo) # Output: Hello! Hello! Hello!
# Indexing (getting one character by position)
message = "Python"
print("First letter:", message[0]) # Output: P
print("Last letter:", message[-1]) # Output: n
# Slicing (getting part of the string)
print("First 3 letters:", message[0:3]) # Output: Pyt
print("From 3rd letter to end:", message[2:]) # Output: thon
# String Methods
greeting = "hello world"
print("Uppercase:", greeting.upper()) # Output: HELLO WORLD
print("Capitalized:", greeting.capitalize()) # Output: Hello world
print("Replace:", greeting.replace("world", "AK1731")) # Output: hello AK1731
# Length of string
print("Length:", len(greeting)) # Output: 11
# Check if a substring is in a string
print("Contains 'world'?","world" in greeting) # Output: True
# String Formatting
name = "AK1731"
language = "Python"
intro = f"My name is {name} and I am learning {language}."
print("Formatted String:", intro)