Python Basic Tutorial Learning-String

Keywords: ascii Python

Character string

Character string

All standard sequence operations (index, slice, multiplication, membership check, length, minimum, and maximum) apply to strings, but don't forget that strings are immutable, so all element and slice assignments are illegal

>>> website = 'http://www.python.org'
>>> website[-3:] = 'com'
Traceback (most recent call last):
File "<pyshell#19>", line 1, in ?
website[-3:] = 'com'
TypeError: object doesn't support slice assignment

String Format

Reference PDF Documentation

String Method

Although the string method completely covers up the wind of the module string, the module contains constants and functions that the string does not have.
string.digits: A string containing numbers 0-9.
string.ascii_letters: A string containing all ASCII letters (upper and lower case).
string.ascii_lowercase: A string containing all lowercase ASCII letters.
string.printable: A string containing all printable ASCII characters.
string.punctuation: A string containing all ASCII punctuation characters.
string.ascii_uppercase: A string containing all uppercase ASCII letters.
# While referring to ASCII characters, the value is actually an uncoded Unicode string.
 1. center  Center the string by adding padding characters on both sides (default is space)
	>>> "The Middle by Jimmy Eat World".center(39)
	' The Middle by Jimmy Eat World '
	>>> "The Middle by Jimmy Eat World".center(39, "*")
	'*****The Middle by Jimmy Eat World*****'
	# Reference ljust, rjust, and zfill
	
 2. find Find a substring in a string.If found, returns the index of the first character of the substring, otherwise returns -1
	>>> 'With a moo-moo here, and a moo-moo there'.find('moo')
	7
	>>> title = "Monty Python's Flying Circus"
	>>> title.find('Monty')
	0
	>>> title.find('Python')
	6
	>>> title.find('Flying')
	15
	>>> title.find('Zirquss')
	-1
	
	#You can also specify the start and end points of the search (both optional).
	>>> subject = '$$$ Get rich now!!! $$$'
	>>> subject.find('$$$')
	0
	>>> subject.find('$$$', 1) # Only the starting point was specified
	20
	>>> subject.find('!!!')
	16
	>>> subject.find('!!!', 0, 16) # Both start and end points are specified
	-1
	
	# Reference rfind, index, rindex, count, startswith, endswith
	
 3. join Elements used to merge sequences Note: The elements of the merged sequence must all be strings
 	>>> seq = [1, 2, 3, 4, 5]
	>>> sep = '+'
	>>> sep.join(seq) # Try merging a list of numbers
	Traceback (most recent call last):
	File "<stdin>", line 1, in ?
	TypeError: sequence item 0: expected string, int found
	>>> seq = ['1', '2', '3', '4', '5']
	>>> sep.join(seq) # Merge a list of strings
	'1+2+3+4+5'
	>>> dirs = '', 'usr', 'bin', 'env'
	>>> '/'.join(dirs)
	'/usr/bin/env'
	>>> print('C:' + '\\'.join(dirs))
	C:\usr\bin\env
	
 4. lower Returns the lowercase version of the string.
	>>> 'Trondheim Hammer Dance'.lower()
	'trondheim hammer dance'
	
	>>> name = 'Gumby'
	>>> names = ['gumby', 'smith', 'jones']
	>>> if name.lower() in names: print('Found it!')
	...
	Found it!
	>>>
	
	#  Tile converts a string to initial capitalization, where all the first letters of a word are capitalized and all other letters are lowercase
	>>> "that's all folks".title()
	"That'S All, Folks"
	
	//Another way is to use the function capwords in the module string.
	>>> import string
	>>> string.capwords("that's all, folks")
	That's All, Folks"
	
	# Reference islower, istitle, isupper, translate
	
 5.  extend Allows you to append multiple values to the end of the list at the same time
	>>> a = [1, 2, 3]
	>>> b = [4, 5, 6]
	>>> a.extend(b)
	>>> a
	[1, 2, 3, 4, 5, 6]
	
	>>> a = [1, 2, 3]
	>>> b = [4, 5, 6]
	>>> a + b
	[1, 2, 3, 4, 5, 6]
	>>> a
	[1, 2, 3]
	# The stitched list is exactly the same as the expanded list from the previous example, but here a has not been modified
	
 6.  replace Specifies that all substrings are replaced by another string and returns the replaced result
	>>> 'This is a test'.replace('is', 'eez')
	'Theez eez a test'
	
 7.  split Used to split strings into sequences.
	>>> '1+2+3+4+5'.split('+')
	['1', '2', '3', '4', '5']
	>>> '/usr/bin/env'.split('/')
	['', 'usr', 'bin', 'env']
	>>> 'Using the default'.split()
	['Using', 'the', 'default']

	# Reference: partition, rpartition, rsplit, splitlines
	
 8. strip  Deletes the blanks at the beginning and end of the string, but not in the middle, and returns the deleted result.
	>>> ' internal whitespace is kept '.strip()
	'internal whitespace is kept'

Determines whether a string meets a specific condition

Many string methods begin with is, such as isspace, isdigit, and isupper.
They determine whether a string has specific properties (such as blank, numeric, or uppercase characters).
These methods return True if the string has specific properties, or False if it does not.

Reference isalnum isalpha isdecimal isDigit isidentifier islower IsNumeric isprintable isspace istitle isupper

Posted by Valkrin on Sun, 15 Sep 2019 19:30:03 -0700