python learning, String

Continue learning python strings

1
2
name = 'Hello, front club'
blog_name = "Hello, front club"

String is a common data type in Python, usually single or double quotes

String access

Accessing elements in a string by subscript index

1
2
3
4
name = 'Hello world!'

print(name[0])
print(name[1: 3])

See the execution results:

1
2
3
[me@VM_0_14_centos python] # python helloworld.py
H
el

String operations

String supports a variety of calculation methods

+ string concatenation

1
2
3
4
name1 = 'Hello'
name2 = 'world!'

print(name1 + name2)

Results of the

1
2
[me@VM_0_14_centos python] # python helloworld.py
Hello world!

* repeat string

1
2
3
name = 'what'

print(name * 3)

Results of the:

1
2
[me@VM_0_14_centos python] # python helloworld.py
whatwhatwhat

in member operator

1
2
3
4
name = 'Hello, front club'

print('H' in name)
print('h' in name)

Results of the:

1
2
3
[me@VM_0_14_centos python] # python helloworld.py
True
False

‘H’ exists in the string pointed to by name, so it is True
‘h’ does not exist in the name variable, so it is False

% string formatting

1
2
3
name = 'Front Club'

print('Hello,% s'% name)

Results of the:

1
2
[me@VM_0_14_centos python] # python helloworld.py
Hello, front club

% s is a placeholder for the string, which means that the subsequent connected string will be output here

Persist in original technology sharing, your support will encourage me to continue to create!