Fundamentals 3 min read

Python Basics: Common String and List Operations with Code Examples

This learning note demonstrates essential Python list and string techniques, including list concatenation with '+', membership testing using 'in' and 'not in', and various slicing methods, all illustrated with concrete code snippets and their output.

Lisa Notes
Lisa Notes
Lisa Notes
Python Basics: Common String and List Operations with Code Examples

This learning note (Day 64) introduces common string and list operations in Python, focusing on practical code examples.

1. Merging lists – Use the + operator to concatenate two lists.

2. Membership testing – The in and not in operators check whether an element exists in a list, returning a Boolean value.

3. List slicing – Various slice notations retrieve sub‑lists, including start:stop, start:, :stop, full slice [:], and negative indices.

list1 = [12, 445, 6, 4, 53]
list2 = ['东皇太一', '貂蝉', '张飞', '吕布']
print(list1 + list2)

list3 = ['hello', 'world', '你好', '呵呵', 124, 56]
print('呵呵' in list3)  # True
print('哈哈' in list3)  # False
if 'hello' in list3:
    print('字符串在列表中')
else:
    print('不在列表中')

print(list3[1:4])
print(list2[1:])
print(list3[:3])
print(list3[:])
print(list2[-2:])

Output:

[12, 445, 6, 4, 53, '东皇太一', '貂蝉', '张飞', '吕布']
True
False
字符串在列表中
['world', '你好', '呵呵']
['world', '你好', '呵呵', 124, 56]
['hello', 'world', '你好']
['hello', 'world', '你好', '呵呵', 124, 56]
[124, 56]
PythonstringCode exampleslistbasic operations
Lisa Notes
Written by

Lisa Notes

Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.