# The Python learning portal (18) -- string

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Python determine the string contains only digits

A method is a.isdigit (), but this method for string contains invalid digital sign, more accurate for:

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

import sys
import math

# To determine whether the digital
def isNum(value):
try:
value + 1
except TypeError:
return False
else:
return True

# To determine whether the digital
def isNum2(value):
try:
x = int(value)
except TypeError:
return False
except ValueError:
return False
except Exception, e:
return False
else:
return True

def test1():

a = "123abcDE"
print a.isalnum()       # True, All characters are numbers or letters

a = "abcDE"
print a.isalpha()       # True, All characters are letters

a = "123.3"
print a.isdigit()       # False, All characters are digital

a = "abcde"
print a.islower()       # True, All characters are lowercase

a = "ABCDE"
print a.isupper()       # True, All characters are uppercase

a = "Abcde"
print a.istitle()       # True, All words are capitalized, like the title

a = "\t"
print a.isspace()       # True, All characters are blank character, \t, \n, \r

arr = (1, 2.1, -3, -4.5, '123a', 'abc', 'aBC', 'Abc', 'ABC', '\t')
for a in arr:
print a,isNum(a)

'''
1 True
2.1 True
-3 True
-4.5 True
123a False
abc False
aBC False
Abc False
ABC False
False
'''

for a in arr:
print a,isNum2(a)
'''
1 True
2.1 True
-3 True
-4.5 True
123a False
abc False
aBC False
Abc False
ABC False
False
'''
```
It is more accurate, more wide applicability. But if you have to make sure there are no sign, using the string isDigit () method is more convenient.

Python string operations, such as string replacement, deletion, interception, replication, connection, comparison, find, including, case conversion, segmentation

```def test2():

# Copy the string
str1 = "ithomer.net"
str2 = str1
str1 = "blog"
print str1, str2        # blog ithomer.net

# Connection string
str1 = "ithomer.net"
str2 = "blog."
str2 += str1
print str1, str2        # ithomer.net blog.ithomer.net

# Find the character
str1 = "ithomer.net"
str2 = ".net"
pos = str1.index(str2)
print pos               # 7

# String comparison
str1 = "blog.ithomer.net"
str2 = "forum.ithomer.net"
pos = cmp(str1, str2)
print pos               # -1

# It contains the specified character
str1 = "blog.ithomer.net"
str2 = ".ithomer."
pos = len(str1 and str2)
print pos               # 9

# The length of the string
str1 = "blog.ithomer.net"
pos = len(str1)
print pos               # 16

# The string case conversion
str1 = "blog.ithomer.net"
str2 = "BLOG.ithomer.NET"
print str1.upper()      # BLOG.ITHOMER.NET
print str2.lower()      # blog.ithomer.net

# Appends the string specified length
str1 = "blog.ithomer.net"
str2 = "1234567"
n = 3
str1 += str2[0:n]
print str1,str2         # blog.ithomer.net123 1234567

# The string specified length comparison
str1 = "blog.ithomer.net"
str2 = "blog.ithomer.NET"
n = 11
print cmp(str1[0:n], str2[0:n])     # 0

# Copy the specified length character
str1 = "blog.ithomer.net"
str2 = ""
n = 12
print str1[0:n]         # blog.ithomer

# The replacement string of n characters to a specified character.
str1 = "blog.ithomer.net"
ch = 'r'
n = 3
print n*ch + str1       # rrrblog.ithomer.net

# Scan string
sStr = 'cekjgdklab'
sStr1 = 'gka'
nPos = -1
for c in sStr1:
print c
print nPos

# Reversing a string
str1 = "ithomer.net"
str2 = str1[::-1]
print str1,str2         # ithomer.net ten.remohti

# The search string
str1 = "ithomer.net"
str2 = ".net"
pos = str1.find(str2)
print pos               # 7

# Splits the string
str1 = "blog.ithomer.net"
str2 = "."
str3 = str1[str1.find(str2) + 1:]
print str3              # ithomer.net
print str1.split(str2)  # ['blog', 'ithomer', 'net']

# Connection string
str1 = ['blog', 'ithomer', 'net']
str2 = "."
str3 = str2.join(str1)
print str3              # blog.ithomer.net

# Show only letters and numbers
str1 = "521.ithomer.NET \$ @ # ! 1314"
fmt = "abcdefghijklmnopqrstuvwxyz0123456789"
for c in str1:
if not c in fmt:
str1 = str1.replace(c,'')
print str1              # 521ithomer1314```

Reference recommendation:

Python string manipulation

Python difflib

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Victor at December 09, 2013 - 11:43 AM