python 字符串大小写转换(不能使用swapcase()方法)

python 3字符串大小写转换

要求不能使用swapcase()方法

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

str1 = input("请输入字符串:")

list1 = list(str1)
str2 = ‘‘

for i in list1:
    if int(ord(i)) >= 65 and int(ord(i)) <= 90:   #大写
        str2 += chr(int(ord(i))+32)           #字符串拼接
    elif int(ord(i)) >= 97 and int(ord(i)) <= 122:  #小写
        str2 += chr(int(ord(i)) - 32)

print(str2)

  

原文地址:https://www.cnblogs.com/hiuhungwan/p/9210881.html

时间: 2024-08-03 05:09:07

python 字符串大小写转换(不能使用swapcase()方法)的相关文章

python字符串大小写转换

1.把字符串中的全部字母转换成小写 str.lower() 例子: # str.lower() 小写 str = "ABC" print(str.lower()) 2.把字符串中的全部字母转换成大写 str.upper() 例子: # str.upper() 大写 str2 = "abc" print(str2.upper()) 3.把首字母转换成大写 str.title() 例子: # str.title() 首字母大写 str3 = "abc"

字符串大小写转换(三种方法)

//直接通过转换比较:function num1($str){ $num = strlen($str); $res = ''; for($i=0;$i<$num;$i++){ if(strtolower($str[$i]) == $str[$i]){ $res .= strtoupper($str[$i]); }else{ $res .= strtolower($str[$i]); } } return $res;}echo num1($a);echo "<hr/>"

php实现兼容Unicode文字的字符串大小写转换strtolower()和strtoupper()

前言 网上流传着这么一个腾讯笔试题: PHP的strtolower()和strtoupper()函数在安装非中文系统的服务器下可能会导致将汉字转换为乱码,请写两个替代的函数实现兼容Unicode文字的字符串大小写转换. 举个例子,我们直接对中英文混排的字符串处理是会出乱码的,如: php > $a = 'abc中华ABC'; php > var_dump(strtoupper($a)); string(12) "ABC??ō?ABC" php > 我们知道中文在 UT

Python字符串拼接、截取及替换方法总结

字符串拼接: 用字符串的join方法: a = ['a','b','c','d'] content = '' content = ''.join(a) print content content的结果:'abcd' 用字符串的替换占位符替换: a = ['a','b','c','d'] content = '' content = '%s%s%s%s' % tuple(a)  print content content的结果是:'abcd' 字符串截取: python的字串列表有2种取值顺序 1

boost 字符串大小写转换

示例代码如下: 1 #include <boost/algorithm/algorithm.hpp> 2 #include <iostream> 3 using namespace std; 4 #include <string> 5 6 void TimerTest() 7 { 8 // 字符串大小写转换; 9 string strTemp = "asdQWEghhh"; 10 string strTemp1 = strTemp; 11 strin

python字符串替换的2种有效方法

python 字符串替换可以用2种方法实现:1是用字符串本身的方法.2用正则来替换字符串 下面用个例子来实验下:a = 'hello word'我把a字符串里的word替换为python1用字符串本身的replace方法a.replace('word','python')输出的结果是hello python 2用正则表达式来完成替换:import restrinfo = re.compile('word')b = strinfo.sub('python',a)print b输出的结果也是hell

php 字符串大小写转换

strtoupper().strtolower().ucfirst().ucfirst().ucwords().mb_strtoupper().mb_strtolower()和mb_convert_case()这八个函数的区别和联系: 函数名称 使用范围 功能 strtoupper PHP4.PHP5 将字符串转化为大写 strtolower PHP4.PHP5 将字符串转化为小写 ucfirst PHP4.PHP5 将字符串的首字母转化为大写 lcfirst PHP5>= 5.3.0 将字符串

JS字符串大小写转换实现方式

toLocaleUpperCase 方法:将字符转换为大写 stringVar.tolocaleUpperCase( ) 必选的 stringVar 引用是一个 String 对象,值或文字. //转换成大写toUpperCase 方法返回一个字符串,该字符串中的所有字母都被转化为大写字母. strVariable.toUpperCase( )"String Literal".toUpperCase( ) 说明toUpperCase 方法对非字母字符不会产生影响. toLocaleLo

Python初学者笔记(3):输出列表中的奇数/奇数项,字符串中的偶数项,字符串大小写转换

[1]a=[8,13,11,6,26,19,24]1)请输出列表a中的奇数项2)请输出列表a中的奇数 解:1) 1 a=[8,13,11,6,26,19,24] 2 print a[::2] Result:>>>[8, 11, 26, 24] 2) 1 a = [8,13,11,6,26,19,24] 2 b = [] 3 for item in a: 4 if item%2 !=0: 5 b.append(item) 6 else: 7 continue 8 print b Resul