Basic methods of strings

#!/usr/bin/env python
# -*- coding:utf-8 -*-

 

# Integer, int
# In Python3, 1234123123123123123123123123123123123
# In Python2, 1234123112
# Long Integer, long
# In Python2, 12341231124321342342 long
# ======== Python3 =========
# Integer int
# In Python3 , 1234123123123123123123123123123123123
# a = 111
# a.bit_length()
# string str
# s1 = "alex"
# s2 = "root"
# s1.title()
# s1.upper()
# s1.startswith('xx')
# list list

#Original tuple

# dictionary dict

# boolean bool


# a = "123a"
# print(type(a),a)
#
# b = int(a)
# print(type(b),b)

# num = "b"
# v = int(num, base=16)
# print(v)

# age = 5
# 1 1
# 2 10
# 3 11
# 4 100
# 5 101
# Current number in binary, at least n bits
# r = age.bit_length()
# print(r)

# test = "aLex"
# Capitalize
# v = test.capitalize()
# print(v)

# All lowercase, casefold is more powerful, many unknown pairs are lowercase accordingly
# v1 = test.casefold()
# print(v1)
# v2 = test.lower()
# print(v2)

# Set the width and center the content
# 20 refers to the total length
# * Blank unknown padding, one character, optional
# v = test.center(20,"middle")
# print(v)

# Go to the string to find the number of occurrences of the subsequence
# test = "aLexalexr"
# v = test.count('ex')
# print(v)

# test = "aLexalexr"
# v = test.count('ex',5,6)
# print(v)

 

# ends
with what # starts with what
# test = "alex"
# v = test.endswith('ex')
# v = test.startswith('ex')
# print(v)

# The expandtabs() method converts the tab symbol ('\t') in the string to spaces, and the default number of spaces for the tab symbol ('\t') is 8
# test = "12345678\t9"
# v = test.expandtabs (6)
# print(v,len(v))

# Search backwards from the beginning, after finding the first one, get its unknown
# > or >=
# test = "alexalex"
# not found -1
# v = test.find('ex')
# print(v)

# The index is not found, the error is ignored
# test = "alexalex"
# v = test.index('8')
# print(v)


# Format, replace placeholders in a string with the specified value
# test = 'i am {name}, age {a}'
# print(test)
# v = test.format(name='alex' ,a=19)
# print(v)

# test = 'i am {0}, age {1}'
# print(test)
# v = test.format('alex',19)
# print(v)

# Format, passed in values ​​{"name": 'alex', "a": 19}
# test = 'i am {name}, age {a}'
# v1 = test.format(name='df' ,a=10)
# v2 = test.format_map({"name": 'alex', "a": 19})

# Does the string contain only letters and numbers
# test = "123"
# v = test.isalnum()
# print(v)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324438335&siteId=291194637