Swapping string case in Python [duplicate] - python

This question already has answers here:
How can I invert (swap) the case of each letter in a string?
(8 answers)
Closed 7 years ago.
I'm rank new in Python, thus the question,
I'm trying to solve a simple problem, where the program takes in a simple string and swaps all the cases. Thus if we enter
SimPLE
We should get
sIMple
This is my code
def main():
oldStr = input()
for s in oldStr:
if s.islower():
s.upper()
elif s.isupper():
s.lower()
print(oldStr)
if __name__ == "__main__" : main()
It just returns the same string. Any help appreciated.

As a generator expression:
mystr = "SimPLE"
print("".join(c.upper() if c.islower() else c.lower() for c in mystr))
The breakdown of the above is:
c.upper() if c.islower() else c.lower()
is an conditional expression that will convert a character from upper to lower case and vice versa.
Then,
(... for c in mystr)
is a generator expression, which is somewhat like a list that is generated on-the-fly.
Finally:
".join(...)
will join any sequence of strings together with nothing ("") between them.

Do this in one fell swoop with a string join on a list comprehension of individual characters:
outstr = ''.join([s.upper() if s.islower() else s.lower() for s in oldStr])
print(outstr)
Input & Output:
sIMple
SimPLE

Strings are immutable. What this means is that when you use the function s.upper(), it is not setting that letter in str to be uppercase, it simply returns that letter in uppercase.
Here is some code that works:
def main():
oldStr = input()
newStr = ""
for s in oldStr:
if s.islower():
newStr+=s.upper()
elif s.isupper():
newStr+=s.lower()
print(newStr)
Notice now that we are creating a new string and simply adding the letters at each point in the forloop as opposed to changing those letters in str.

You are running each character through lower() and upper(), but these functions do not change the character.
Instead, they return the modified version of the character. The original character s will stay as it is.
You should build a new string based off the return values of lower() and upper(), and return that string.

1) you need to put the main() call on new line, as python relies on whitespace heavily for program structure
2) s is a temporary variable created for the purpose of the for statement. It doesn't actually reference the character in the string
Essentially what is going on is that s has the same value as the character in the string, but it IS NOT ACTUALLY the character in the string.

Related

Print a string without any other characters except letters, and replace the space with an underscore

I need to print a string, using this rules:
The first letter should be capital and make all other letters are lowercase. Only the characters a-z A-Z are allowed in the name, any other letters have to be deleted(spaces and tabs are not allowed and use underscores are used instead) and string could not be longer then 80 characters.
It seems to me that it is possible to do it somehow like this:
name = "hello2 sjsjs- skskskSkD"
string = name[0].upper() + name[1:].lower()
lenght = len(string) - 1
answer = ""
for letter in string:
x = letter.isalpha()
if x == False:
answer = string.replace(letter,"")
........
return answer
I think it's better to use a for loop or isalpha () here, but I can't think of a better way to do it. Can someone tell me how to do this?
For one-to-one and one-to-None mappings of characters, you can use the .translate() method of strings. The string module provides lists (strings) of the various types of characters including one for all letters in upper and lowercase (string.ascii_letters) but you could also use your own constant string such as 'abcdef....xyzABC...XYZ'.
import string
def cleanLetters(S):
nonLetters = S.translate(str.maketrans('','',' '+string.ascii_letters))
return S.translate(str.maketrans(' ','_',nonLetters))
Output:
cleanLetters("hello2 sjsjs- skskskSkD")
'hello_sjsjs_skskskSkD'
One method to accomplish this is to use regular expressions (regex) via the built-in re library. This enables the capturing of only the valid characters, and ignoring the rest.
Then, using basic string tools for the replacement and capitalisation, then a slice at the end.
For example:
import re
name = 'hello2 sjsjs- skskskSkD'
trans = str.maketrans({' ': '_', '\t': '_'})
''.join(re.findall('[a-zA-Z\s\t]', name)).translate(trans).capitalize()[:80]
>>> 'Hello_sjsjs_skskskskd'
Strings are immutable, so every time you do string.replace() it needs to iterate over the entire string to find characters to replace, and a new string is created. Instead of doing this, you could simply iterate over the current string and create a new list of characters that are valid. When you're done iterating over the string, use str.join() to join them all.
answer_l = []
for letter in string:
if letter == " " or letter == "\t":
answer_l.append("_") # Replace spaces or tabs with _
elif letter.isalpha():
answer_l.append(letter) # Use alphabet characters as-is
# else do nothing
answer = "".join(answer_l)
With string = 'hello2 sjsjs- skskskSkD', we have answer = 'hello_sjsjs_skskskSkD';
Now you could also write this using a generator expression instead of creating the entire list and then joining it. First, we define a function that returns the letter or "_" for our first two conditions, and an empty string for the else condition
def translate(letter):
if letter == " " or letter == "\t":
return "_"
elif letter.isalpha():
return letter
else:
return ""
Then,
answer = "".join(
translate(letter) for letter in string
)
To enforce the 80-character limit, just take answer[:80]. Because of the way slices work in python, this won't throw an error even when the length of answer is less than 80.

Can I uppercase particular letter in string in Python? [duplicate]

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 3 years ago.
Lest say I have string like this:
string = "hello world"
I want to have only praticular letter uppercased so if i choose second letter my output should look like this:
hEllo wold
But if i try for example:
string[1].upper()
My output is:
hello world
And not:
hEllo wold
I dont know why...
This line:
string[1].upper()
Isn't doing what you think. Sure, it uppercases the value in string[1], but it returns a new string with the result! - because in Python strings are immutable. Now, you'd be tempted to think that this would fix it:
string[1] = string[1].upper()
... But no, this will fail because once again - strings are immutable. You have no choice but to create a new string, with some logic to tell which positions you want to uppercase. For example, to uppercase the value at index = 1:
''.join(c.upper() if i == 1 else c for i, c in enumerate(string))
To capitalize only one letter of a string you have to capitalize the part of the string starting from the nth position, and assign it back to the variable:
string = string[:n] + string[n:].capitalize()
In Python Strings are not Modifiable hence the operation that you specified would not work.
In order to make this work,you can convert the string to a list and then convert a particular character to uppercase and then join the result back into a string.
sample_str="hello world"
sample_list=list(sample_str)
sample_list[2]=sample_list[2].upper()
print(''.join(str(i) for i in sample_list))

Capital letters in Jython/JES

I am currently writing a JES program that returns True or False dependent on whether a string containing a palindrome is passed to it. Although the program works, it fails when a capital letter or punctuation symbol is present. How could I get it to work?
print(ThisPalindrome("racecar"))
>> True
print(ThisPalindrome("Racecar"))
>> False
To solve the issue with capitalization, you could try using the str.lower() method in your checks.
def ThisPalindrome(word):
lowercase = word.lower()
reversedOrder = reversed(lowercase)
if lowercase == ''.join(reversedOrder):
return True
else:
return False
In theory, this function should work with basic punctuation too, as long as it doesn't break the function. Input such as ' could cause it to break.
The toLowerCase() method to return the calling string value converted to lowercase.
The replace() method to return a new string with some or all matches of a pattern replaced by a replacement. We will use one of the RegExp we just created earlier.
The split() method splits a String object into an array of strings by separating the string into sub strings.
The reverse() method reverses an array in place. The first array element becomes the last and the last becomes the first.

Python treats + as a digit?

Basically im making a small piece of code to remove the end character of a string until the string passes .isdigit.
def string_conversion(string):
for i in string:
if i.isdigit == False:
string = string[0:len[string]-1]
print(string) #This is just here to see if it goes through
test = "60+"
string_conversion(test)
I used http://www.pythontutor.com/visualize.html to see why I wasn't getting any output, it passed a + symbol as a digit, just like the numbers 6 and 0.
Am I doing something wrong or does python treat + as a digit?
str.isdigit is a method not an attribute, so (as was already mentioned in the comments) you need to call it.
Also you check each character (starting from the left side) for isdigit but if it's not passing the isdigit() test you remove a character from the right side. That doesn't seem right. You probably wanted to iterate over the for i in reversed(string) (or using slicing: for i in string[::-1]).
But you also could simplify the logic by using a while loop:
def string_conversion(string):
while string and not string.isdigit():
string = string[:-1]
return string
def string_conversion(string):
for i, s in enumerate(string):
if s.isdigit() == False:
string = string[:i]
break
print(string) #This is just here to see if it goes through
test = "60+"
string_conversion(test)
Try This

a mistake I keep having with for loops and return statements

I have been noticing a problem I am having whenever I try to make a function that takes changes a string or a list then returns it.
I will give you an example of this happening with a code I just wrote:
def remove_exclamation(string):
string.split(' ')
for i in string:
i.split()
for char in i:
if char == '!':
del char
''.join(i)
' '.join(string)
return string
For instance, I create this code to take a string as its parameter, remove any exclamation in it, the return it changed. The input and output should look like this:
>>>remove_exclamation('This is an example!')
'This is an example'
But instead I get this:
>>>remove_exclamation('This is an example!')
'This is an example!'
The function is not removing the exclamation in the output, and is not doing what I intended for it to day.
How can I keep avoiding this when I make for loops, nested for loops etc?
You write your code and formulate your question as if it was possible to modify strings in Python. It is not possible.
Strings are immutable. All functions which operate on strings return new strings. They do not modify existing strings.
This returns a list of strings, but you are not using the result:
string.split(' ')
This also:
i.split()
This deletes the variable named char. It does not affect the char itself:
del char
This creates a new string which you do not use:
''.join(i)
This also:
' '.join(string)
All in all, almost every line of the code is wrong.
You probably wanted to do this:
def remove_exclamation(string):
words = string.split(' ')
rtn_words = []
for word in words:
word_without_exclamation = ''.join(ch for ch in word if ch != '!')
rtn_words.append(word_without_exclamation)
return ' '.join(rtn_words)
But in the end, this does the same thing:
def remove_exclamation(string):
return string.replace('!', '')
Without clearly knowing the intentions of your function and what you are attempting to do. I have an alternative to the answer that zvone gave.
This option is to remove any characters that you have not defined in an allowed characters list:
characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
test_string = "This is an example!"
test_string = ''.join(list(filter(lambda x: x in characters, test_string)))
print(test_string)
This outputs:
This is an example
Note, this is the Python 3 version.
Python 2, you do not need the ''.join(list())
Doing it this way would allow you to define any character that you do not want present in your string, and it will remove them.
You can even do the reverse:
ignore_characters= "!"
test_string = "This is an example!"
test_string = ''.join(list(filter(lambda x: x not in ignore_characters, test_string)))
print(test_string)
Strings are immutable in Python. And you cannot change them. You can however, re-assign there values.
That is where your problem lies. You never reassign the value of your strings, when you call .split() on them.
But there are also others errors in your program such as:
Your indention
The fact that your just returning the string thats passed into the function
Your use of the del statement
etc.
Instead, create a new string by iterating through the old one and filtering out the character(s) you do not want, via list comprehension and ''.join().
def remove_exclamation(string):
return ''.join([char for char in string if char != '!'])
But as #Moses has already said in the comments, why not just use str.replace()?:
string = string.replace('!', '')
def remove_exclamation(string):
#you think you are splitting string into tokens
#but you are not assigning the split anywhere...
string.split(' ')
#and here you are cycling through individual _chars_ in string which was not affected by the split above ;-)
for i in string:
#and now you are splitting a 1-char string and again not assigning it.
i.split()
And string is still your input param, which I assume is of type str. And immutable.
On top of which, if you were import/using the string module, you would be shadowing it
A big part of your confusion is knowing when the methods mutate the objects and when they return a new object. In the case of strings, they never mutate, you need to assign the results to a new variable.
On a list however, and the join() somewhere makes me think you want to use a list, then methods generally change the object in place.
Anyway, on to your question:
def remove_exclamation(inputstring, to_remove="!"):
return "".join([c for c in inputstring if c != to_remove])
print (remove_exclamation('This is an example!'))
output:
This is an example

Categories

Resources